Is this correct? (redirecting /index.php --> /)

Using mod_rewrite to handle various content issues

Is this correct? (redirecting /index.php --> /)

Postby Sarge » Fri Feb 01, 2008 5:01 am

I'm using this to redirect domain.com to www.domain.com and /index.php to /:

Code: Select all
# Redirect domain.com/* --> www.domain.com/*
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

# Redirect /index.php --> /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ [NC]
RewriteRule ^index\.php$ http://www.domain.com/ [R=301,L]


It's working fine as far as I can tell. But I'd like to know, is it correct or is there room for improvement?
Sarge
 
Posts: 12
Joined: Fri Jan 19, 2007 1:52 am

Postby richardk » Fri Feb 01, 2008 4:07 pm

They're fine. To be slightly more efficient you can put them the other way round as when removing index.php it adds www in the same rule.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Sarge » Fri Mar 07, 2008 3:06 am

Hmmm. I noticed today that redirection of index.php wasn't working and I don't know why. Strange, it was working fine before. I switched recently to Apache 2.2.8, could it be the issue?

domain.com gets redirected to www.domain.com just fine. The index.php part is what doesn't work.

domain.com/index.php gets redirected to www.domain.com/index.php and www.domain.com/index.php stays the same (surprise, surprise...) I switched the rules around, still with the same result.

I'm now using this and it seems to work (feels faster too):
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ http://www.domain.com/$1 [R=301,L]

The code for www is still the same.

Do you think there could be any possible issues?
Sarge
 
Posts: 12
Joined: Fri Jan 19, 2007 1:52 am

Postby richardk » Fri Mar 07, 2008 4:27 pm

Is it working now? Is the mod_rewrite in a .htaccess file?
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Redirect /index.php --> /
RewriteCond %{THE_REQUEST} ^[a-z]{3,9}\ (/[^\ ]+)?/index\.php(\?.*)?\ [NC]
RewriteRule ^(.+/)?index\.php$ http://www.domain.com/ [R=301,L]

# Redirect domain.com/* --> www.domain.com/*
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Sarge » Sun Mar 09, 2008 8:29 am

Of course the rewrite is in .htaccess.

Your last rewrite works only if I replace [NC] with HTTP/ and [a-z] wasn't working. I also have subdirectories. So I used this:

Code: Select all
# Redirect /index.php --> /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ (/[^\ ]+)?/index\.php(\?.*)?\ HTTP/
RewriteRule ^(.+/)?index\.php$ http://www.domain.com/$1 [R=301,L]


The only problem is that the AJAX-based post preview in my SMF forum doesn't work (the contents is empty).

This works fine:
Code: Select all
# Redirect /index.php --> /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ http://www.domain.com/$1 [R=301,L]


With either of the above, index.php gets redirected, but index.php? doesn't. And neither works with [NC], should it be a problem?

Thanks for your help so far!
Sarge
 
Posts: 12
Joined: Fri Jan 19, 2007 1:52 am

Postby richardk » Mon Mar 10, 2008 3:50 pm

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Redirect /index.php --> /
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php(\?.*)?\  [NC]
RewriteRule ^(.+/)?index\.php$ http://www.domain.com/%1 [NC,R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Sarge » Mon Mar 10, 2008 9:05 pm

The above doesn't work with AJAX post previews either.

Also, index.php? is rewritten as ? which is still not good enough.

Also, only index.php and index.php? should be rewritten, not index.php?*.
Sarge
 
Posts: 12
Joined: Fri Jan 19, 2007 1:52 am

Postby richardk » Thu Mar 13, 2008 12:53 pm

does your AJAX request URLs with index.php in it?
Do you have URLs like .../index.php?query=string?

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php\?\  [NC]
RewriteRule ^(.+/)?index\.php$ http://www.domain.com/%1? [NC,R=301,L]

RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php(\?.+)?\  [NC]
RewriteRule ^(.+/)?index\.php$ http://www.domain.com/%1 [NC,R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Content

Who is online

Users browsing this forum: No registered users and 1 guest

cron