Htaccess redirection based on condition

Hi

I have a htaccess rule to show a person’s profile on my site. The rule used is:
RewriteRule profile/([-a-z|A-Z]+)/([0-9]+)$ /profile/$1/$2/ [R=301,L]
RewriteRule profile/([-a-z|A-Z]+)/([0-9]+)/$ /newprofile\.php?name=$1&start=$2

This rule is for profiles I have approved. Now what I want to do is put all required profiles on my site and let that person come and say it is my profile. So the profile is of anonymous user and this profile page I want to display using another page with different htaccess rule set like:

RewriteRule newprofile/([-a-z|A-Z]+)/([0-9]+)$ /newprofile/$1 [R=301,L]
RewriteRule newprofile/([-a-z|A-Z]+)/([0-9]+)/$ /anonymous\.php?name=$1

i.e. passing certain new parameters. Now when a person approves his profile then I want the earlier htaccess rule to be set. So it is something like setting if…else condition in htaccess. Please tell me if it is possible or is there another way to what I want? I do not want a redirect in profile page to the newprofile page.

thanks.
manju

I wouldn’t put that kind of logic in htaccess. But I’m not sure I really understand: what is an anonymous profile? It must belong to something or it wouldn’t be a profile.

When it comes to conditions in redirections, a typical approach is to let PHP do the handling.

For example, in a typical procedural system you could route EVERY request (which links to a non-existent path on the server) to a PHP file. The PHP file grabs the request string and processes it to determine what to do next. It could then simply include the relevant file:
.htaccess


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) Route.php?path=$1

Route.php:

include('db_connect.php');
$Path = explode('/', $_GET['path']);
if(count($Path) > 0){
    switch($Path[0]){
        case 'profile':
            $Name = $Path[1];
            if(profileIsApproved($Name)){
                include('profile.php');
            }else{
                include('newprofile.php');
            }
        break;
        //etc
    }
}

This is more straightforward with an MVC approach, though judging by your rewrites it appears you are using the procedural approach, which is fine :slight_smile:

RewriteRule newprofile/([-a-z|A-Z]+)/([0-9]+)$ /newprofile/$1 [R=301,L]

I wonder, why there is no $2/ as in the first rule

anyway, i’d be glad to take a look on example case to be sure what thelittlemermaid is really want
I am sure the solution is very simple

I do not understand htaccess very well. The first rule was set up by someone earlier working on this project. And now I want to set up the same rule making little modifications to it and finally redirecting back to the first one.

In simple terms, there is one rewrite rule used currently. Now there will be another rewrite rule for anonymous profiles for some time and then they will be redirected again to the same old rewrite.

Hope I am clear, otherwise i will try to explain it another way.

the solution given by Jake Arkinstall seems exactly what i want. I will try it out n let u guys no.

Thanks a lot

you need to use it with precautions.
It can end with unpredictable results.
Because it acts like 404 error handler.

Hi

Can you please suggest a better way then? Or please tell what precautions are to be taken when using Jake’s method? As I already said I am no htaccess expert.

Thanks

I can tell you nothing because I don’t know what you need.
No one knows.
You didn’t even explained what address you want to be translated. No one knows what is relation between terms “approved”, “anonymous” and the string in the address bar.

Google for mod_rewrite because that is what you are asking about, not .htaccess in general. mod_rewrite rules can be placed in an htaccess file, but they can also be placed in the httpd.conf file (and when so placed they are faster, notably so on a high traffic site).

Most php programmers use a very short rewrite rule to transfer control from apache over to php for the path. I personally use a rewrite rule to transfer control only when the file does exist for the directory, then use the htdocs folder in question as a cache folder for the php script (that is, if I have an output I can keep for a while I write it where apache can find it without waking php up at all which is VERY fast).

mod_rewrite lets you map requested URLs to the files on your server that should handle those requests

It does not change any of the links your website outputs. Your redirect rules are not aware of whether a given profile is claimed or not, they can’t have that kind of logic.

I think you’re looking at this problem in reverse and it doesn’t work that way.

I am looking for similar help which thelittlemermaid is explained here.

I want add user name into my new url

old url- mysite/categories/mypage

new url- mysite/categories/mypage-geetap

where geetap is the username

Now my question is how can i redirect my old url to new url directly. i was searching realted to this topic and found 301 redirect method for webpage redirection.

And i tried small test page which works fine.like this

RewriteRule ^myfolder/test1\\.php$ /myfolder/test2.php [R=permanent,L]

So related to my topic ,if geetap has added “mypage” into my site then only i want to redirect that page to my new url otherwise show the old url.(which is sort of unclaimed thing which is explain here)

So i was confused with dans statement and i thought i need to check my conditions with php as well. so is it means using php(?) i need to check if user name is exist or not along with the “mypage”?

am i on the right track? please help me.

Thanks in advance.

So related to my topic ,if geetap has added “mypage” into my site then only i want to redirect that page to my new url otherwise show the old url.(which is sort of unclaimed thing which is explain here)

You need to do this in your PHP code, not with mod_rewrite. mod_rewrite doesn’t know who’s claimed what or what’s in your database. You can’t write a condition on anything that can’t be inferred from the URL, nor does a redirect change your existing links to now point to someone’s name where they didn’t before.

Thanks for your reply dan.

Therotically understood. but how to do this don’t know. :confused:

ok, firstly do you think i should keep two pages profilewithusername.php and profilewithoutusername.php and the redirect it accordingly?

Could you please suggest me some random flow so i can try it?

You have some URLs for some pages that look like this:
mysite/categories/mypage

Once someone “claims” their page, you want everyone to access that page at this URL instead:
mysite/categories/mypage-username

  • TODO: You’ll need whatever code prints links to “mypage” anywhere on your site to now print those links as “mypage-username”. You have to write that code, it won’t happen any way else.

You also presumably have a mod_rewrite rule somewhere that currently maps mysite/categories/mypage to some PHP file on your server that is actually handling those requests.

  • TODO: You need to modify this script to now look up whether “mypage” requested has already been claimed, and if so, redirect to “mypage-username” with the appropriate username. This cannot be handled by a rewrite rule, you need to implement it in code. You can send a 301 redirect header the same way you send the actual location to redirect to in PHP, with header().

If you want to do something special on “mypage-username” that you didn’t do on “mypage” before it was claimed, you have two options.

Either:

  • TODO: Modify the script that handles mypage requests to parse the requested URI ($_SERVER[‘REQUEST_URI’]) to see that “mypage-username” has a username in it (split on the dash) and do that new thing.

OR [list][*]TODO: Write a new rewrite rule that matches “mypage-username” (a regular expression that matches the text before and after the dash and passes them to a script as separate parameters in the query string), and have your script that handles these rewritten requests use the new parameters.[/list]

You can place this rule earlier in your .htaccess file, and give it a [L] flag for “last”, to make sure it fires on URLs with a dash before the existing rule for URLs without a dash.

Ok, I ll go step by step. i am considering your first TODo list, Do you think the following logic should work for it?


//$url= "my_ability" ;-is the requested url for we need to check weather it is claimed or not?

$select = op->runsql(SELECT b.title, a.username, b.url, b.descr, b.bpath 
FROM `binfo` AS b, author AS a
WHERE a.id = b.author
AND b.bpath LIKE '%my_ability%')

 if(mysql_num_rows($select) > 0)
   {

     while($row = $op->select($select))
     {
     
     $username = $row['username'];
     $blist    = $row['title'];
      $burl    = $row['bpath'];
     
     if ($username != "")
     {
     
      echo "<li><a href='/categories_without_username/".$burl."'>".ucfirst(strtolower(stripslashes($blist)))."</a></li>";
     
     }else
     {
     // if someone claimes their pages i am storing bpath with username
     // e.g - my_world-geetap
     // geetap is the username
     echo "<li><a href="/categories_with_username/<?=$bpath;?>" title="<?=html_entity_decode(stripslashes($blist));?>"></a><li>"
     
     }
   
   }
   
 }//if ends here%')

 if(mysql_num_rows($select) > 0)
   {

     while($row = $op->select($select))
     {
     
     $username = $row['username'];
     $blist    = $row['title'];
      $burl    = $row['bpath'];
     
     if ($username != "")
     {
     
      echo "<li><a href='/categories_without_username/".$blogurl."'>".ucfirst(strtolower(stripslashes($blist)))."</a></li>";
     
     }else
     {
     // if someone claimes their pages i am storing bpath with username
     // e.g - my_ability-geetap
     // geetap is the username
     echo "<li><a href="/categories_with_username/<?=$bpath;?>" title="<?=html_entity_decode(stripslashes($blist));?>"></a><li>"
     
     }
   
   }
   
 }//if ends here

Hello Dan,

I am solved with my problem. It was only php condition i needed to check. but your “TODO” list helped me a lot to understand my problem.

If anyone is having same issue please follow dan’s TODO list you ll get your solution. I hope this ll help someone.

Thanks a lot.:slight_smile: