views:

134

answers:

1

Hello all,

I have a site that I don't think my htaccess is implementing 301 redirects properly, I think my problem may be at the application level but I would appreciate it if someone can confirm the following works as I expect it to.

Example redirect: http://www.siteic.com/a/b/c/ should go to http://www.siteic.com/a/b/c - however the below doesn't do that. NOTICE the trailing slash is removed.

Options +FollowSymLinks
RewriteEngine on
ErrorDocument 404 /

RewriteCond %{HTTP_HOST} ^siteic\.com
RewriteRule ^(.*)$ http://www.siteic.com/$1 [R=permanent,L]

RewriteCond {REQUEST_URI} ^/first/$
RewriteRule ^(.*)$ http://www.siteic.com [R=permanent,L]

RewriteCond {REQUEST_URI} ^/first$
RewriteRule ^(.*)$ http://www.siteic.com [R=permanent,L]

RewriteRule ^first/([a-z,\_%,A-Z,\_%-,0-9,\-]*)$ otherst.php?page=$1
RewriteRule ^first/([a-z,\_%,A-Z,\_%-,0-9,\-]*)/([a-z,\_%,A-Z,\_%-,0-9,\-]*)$ otherst.php?page=$1&page2=$2
+1  A: 

I would suspect you first rule to be always true, therefore the subsequent rules are never used.

You should use that instead:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.your_domain.com$
RewriteRule ^(.*)$ http://www.your_domain.com/$1 [R=301]

Your second rule makes sure all uri with the string '/first/' redirect to the homepage. Your third rule does exactly the same with the string '/first' so in fact you could remove your second rule, or your third one depending on your intended behaviour.

Your fourth rule will never match because uri with "first/" will have been redirected to homepage. Your fifth rule should come before your fourth rule because rule 4 will be more often true than rule 5.

As to your request: how to remove the trailing slash, i would do this: - first make sure the path points to a non-existent directory - if true, remove the slash

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]

So in the end i would do this:

RewriteEngine on
# force WWW in the url
RewriteCond %{HTTP_HOST} !^www.siteic.com$
RewriteRule ^(.*)$ http://www.siteic.com/$1 [R=301]

# remove trailing slash if the url points to a non-existing folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]

# redirects all url with /first to otherst.php
## with 2 GET vars
RewriteRule ^first/([a-z,\_%,A-Z,\_%-,0-9,\-]*)/([a-z,\_%,A-Z,\_%-,0-9,\-]*)$ 
otherst.php?page=$1&page2=$2

## with 1 GET var
RewriteRule ^first/([a-z,\_%,A-Z,\_%-,0-9,\-]*)$ otherst.php?page=$1
pixeline
Just tried this and it doesn't solve my problem. :( - It still does what my code did above. It doesn't do a `http://www.siteic.com/a/b/c/` redirect to `http://www.siteic.com/a/b/c` - note the slash. Granted your code is much cleaner, efficient and logical.
Abs
i've updated my answer with your request.
pixeline