views:

338

answers:

2
+2  Q: 

.htaccess Problem

I'm having trouble with redirecting urls using the .htaccess file. This is how my htaccess file looks like:

Redirect 301 /file-name/example.php http://www.mysite.com/file-name/example-001.php
Redirect 301 /section-name/example.php http://www.my-site.com/section-name/example-002.php

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.mysite.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/(.*)$ hqtemplates/articles.php?file_name=$2 [L]
php_value session.use_only_cookies 1
php_value session.use_trans_sid 0

Now the problem is that when I go to page: www.my-site.com/file-name/example.php instead of redirecting me to www.my-site.com/file-name/example-001.php it redirects me to www.my-site.com/file-name/example.php?file_name=example-001.php. For some reason it adds "?file_name=example-001.php" to the url. Anyone know's why this is happening and how to fix this?

+3  A: 

the problem is mixing mod_alias redirect rules with mod_rewrite. the solution is to stick to one. this is a solution for a pure mod_rewrite only approach (i have included only relevant portions to the issue):

RewriteEngine on
# emulate specific mod_alias Redirect rules
#
# Flags explanation:
#   [L] = last rule, stop processing further rules
#   [R=301] = 301 Redirect
#
RewriteRule ^file-name/example.php$ http://www.mysite.com/file-name/example-001.php [L,R=301]
RewriteRule ^section-name/example.php$ http://www.my-site.com/section-name/example-002.php [L,R=301]

# handle other rewrite requests
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/(.*)$ hqtemplates/articles.php?file_name=$2 [L]

as to what's happening in your .htaccess, here's the rewrite log:

(2) rewrite 'file-name/example.php' -> \
        'hqtemplates/articles.php?file_name=example.php'
(2) strip document_root prefix: /home/test/hqtemplates/articles.php -> \
         /hqtemplates/articles.php
(1) internal redirect with /hqtemplates/articles.php [INTERNAL REDIRECT]
(1) pass through /home/test/Sites/file-name/example-001.php

which suggests:

  1. apply mod_rewrite "RewriteRule ^(.+)/(.*)$ hqtemplates..."
  2. file-name/example.php is now equivalent to hqtemplates/articles.php?file_name=example.php
  3. apply mod_alias "Redirect 301..."
  4. show final file-name/example-001.php?file_name=example.php
Owen
awesome! thanks Owen. should've seen that. :P
ocergynohtna
A: 

is there anyway I can fix the problem and still keep the 301 redirects?

ocergynohtna
yes, whether you redirect via mod_alias or mod_rewrite, it's the same thing, i'll change the code to specify 301 redirects in the "all mod_rewrite" solution
Owen
a few notes, you have enough rep to add comments now, that's preferable to adding an answer. the updated rules i posted (emulate specific mod_alias redirect rules) are now equivalent (301 redirects) to your original mod_alias rules
Owen