0

I have a two part problem that I have only been successful with the first part. I have the following listed in .htaccess which works great:

RewriteRule ^senior/?$  demo.php?dID=1 [NC,L] 

So visitors can go straight to mysite.com/senior and the correct internal page (demo.php?dID=1) gets pulled up.

My problem is that I also would like a rewrite where /demo.php?dID=1 shows up in the URL bar as /senior. So existing links show up with the new user friendly url.

My attempt so far has been:

RewriteCond %{QUERY_STRING}           ^dID=1$
RewriteRule ^demo.php$   senior [NC] 
RewriteRule ^senior/?$  demo.php?dID=1 [NC,L] 

Thanks for your time and help.

2 Answers 2

1

You want to match against the request instead of the query string and redirect the browser:

RewriteCond %{THE_REQUEST}  \ /demo\.php\?dID=1($|\ |&)
RewriteRule ^demo.php$   /senior? [NC,R] 
RewriteRule ^senior/?$  /demo.php?dID=1 [NC,L] 
Sign up to request clarification or add additional context in comments.

2 Comments

Actually there's a slight error above. If the ID being passed is 1 or 11, it matches 1, not 11. Same if the id is 8 or 86, it matches 8.
@Ryan see the additional regex element for the condition
0

Place this additional rule before your current rule:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+demo\.php\?dID=([^\s&]+) [NC]
RewriteRule ^ /senior? [R=302,L]

RewriteRule ^senior/?$ /demo.php?dID=1 [NC,L] 

Your current rule based on QUERY_STRING will loop since internal rewrite rule will populate the QUERY_STRING and both rule will keep triggering each other.

2 Comments

I tried your solution also, and it ended up redirecting to the following page: mysite.com/var/www/vhosts/6yfw-lwnd.mysite.com/httpdocs/senior. Jon Lin's solution worked up above, but I greatly appreciate your effort too!
@Ryan: It was actually due to missing leading slash which I have edited now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.