0

I can't seem to get my 301 redirect working, mod_alias is enabled along with mod_rewrite.

My rewrites work fine, but the 301s do not work. Basically what I am trying to do is setup "clean" URLs from dynamic php pages, which works fine...

The other part for each of these dynamic pages, I would like them to redirect to the right "clean" url.

Here is my .htaccess

#
# Options below ensure that all traffic is directed to www.mydomain
#
Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule (.*) http://www.mydomain.com/$1 [R=301,L]
RewriteRule ^index.php$ http://www.mydomain.com/ [R=301,L]

#
# URL Rewrites
#
RewriteRule videos page.php?id=1 [L]

#
# Custom redirects from OLD URLs
#
redirect 301 /page.php?id=1 /videos

Trying to achieve...

mydomain.com/videos

(this works - redirect does not)  

If someone visits the dynamic page (page.php?id=1) they're automatically redirected to the clean URL.

mydomain.com/page.php?id=1 -> mydomain.com/videos

I tried using R=301 in the RewriteRule but a fatal error occurs. So that's why I created the redirect 301, but it just does not work period.

JCastell
  • 1,135
  • 1
  • 10
  • 11

1 Answers1

0

Okay, so if a visitor requests /videos then you want the request to be silently rewritten to the actual path /page.php?id=1 so that you can advertise the "clean" URL /videos and hide the query string ugliness.

But you also want to force a visitor who actually requests /page.php?id=1 to be permanently redirected to the "clean" URL /videos so that they get the message that query string ugliness is no longer for their eyes.

The problem is that even rewritten requests have to run through your Apache directives all over again, which means that clean will rewrite to actual, which will then redirect to clean, which will rewrite to actual, and so on infinitely. Which is why you're getting a fatal error.

The only way to break this loop is to change the actual path or insert a dummy variable which allows the RewriteEngine to stop once the rewrite has been done once.

For example, you could use the following:

RewriteCond %{QUERY_STRING} !&redirect=done$
RewriteCond %{QUERY_STRING} ^id=1
RewriteRule /page.php /videos [R=permanent]
RewriteRule /videos /page.php?id=1&redirect=done

This will always silently rewrite /videos to /page.php?id=1&redirect=done, but it will only redirect /page.php to /videos if the request for page.php does not have a query string which ends with redirect=done (and also does have a query string which begins with id=1).

It's convoluted, but it's the only way I can think to break the infinite loop.

Bobulous
  • 12,967
  • 4
  • 37
  • 68
  • Actually just trying to get the very last line to work. It won't automatically redirect to the clean URL that I made just above (that works) – JCastell Mar 25 '13 at 23:06
  • I'm not clear on what you're trying to do. It's probably best if you edit your question and give examples of what visitors will type into their browser (or how public links will look) and then how the path on the server actually needs to look to find the relevant file or script. At the moment we only have your existing config as a clue, and as that's faulty it's not a great starting point. – Bobulous Mar 26 '13 at 20:14
  • Sorry, figured it was easy to spot. Clean URL (Works) mydomain.com/videos 301 Redirect (does not work) mydomain.com/page.php?id -> mydomain.com/videos – JCastell Mar 27 '13 at 13:01
  • Oh, I think I see. You can't redirect the actual path to a clean path because that will then get silently rewritten to the actual path. Which will then get redirected to the clean path, and so on infinitely. The only way to do such a thing is to create a new actual path so that the RewriteEngine can determine whether to rewrite or not. – Bobulous Mar 27 '13 at 20:01
  • I've completely changed my answer so that it does what I think you're trying to achieve. – Bobulous Mar 27 '13 at 20:12
  • It makes sense, it's what I thought was happening. Conflict with rules, much appreciated for your input. Thanks. – JCastell Mar 27 '13 at 21:38