Forum Moderators: phranque

Message Too Old, No Replies

"This" directory/path for index redirect without domain?

Avoiding static domain for /index.php to / redirect?

         

JAB Creations

12:17 pm on Feb 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've been searching for a way to redirect /index.php to simply / without being forced to specify a domain name. I could probably rewrite (no pun intended) the existing scripts I keep finding (half of them end up being here on the forum) if I could figure this out?

- John

jdMorgan

4:37 pm on Feb 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Instead of hard-coding a value for the domain, use the value of the server variable HTTP_HOST, as long as it is not blank. If HTTP_HOST is blank, then either don't redirect the /index.php request, or use the value of the server variable SERVER_NAME.

This decision should be made based on usability -- Don't confuse the visitor or the search engines!

Jim

JAB Creations

5:46 pm on Feb 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Jim, I think I'm close though now I have an infinite loop. I'm pretty sure...

%{SERVER_NAME}

...is an Apache variable because if I remove the % it redirects to http://www.{server_name}.com/.

I think the closest I've gotten is with the following...

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/

RewriteRule ^index\.php$ [%{SERVER_NAME}...] [R=301,L]

I'm missing something and I've tried a few things (in no particular order)...

1.) %{REQUEST_URI} however this doesn't do anything however it also does not loop...so perhaps it redirects once...or it doesn't redirect at all? Hm...

2.) I've seen $1 used a lot in posts though it doesn't seem to do much. I'm not seeing it in Apache's documentation (not exactly search friendly ha!)

I've tried various combinations though I end up with funny addresses. I'm not sure if I've mentioned that I'm trying to do this for all sub-directories too? What else can I try to mess with?

- John

jdMorgan

6:32 pm on Feb 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See the Apache mod_rewrite documentation at apache.org, particularly, back-references in RewriteRule and RewriteCond, for info on $1-$9 and %1-%9.

Also see the resources cited in our forum charter.

This rule requires a non-blank HTTP_HOST:


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteCond %{HTTP_HOST} ^(.+)$
RewriteRule ^index\.php$ http://%1/ [R=301,L]

Jim

[edited by: jdMorgan at 8:19 pm (utc) on Feb. 24, 2009]

g1smd

7:48 pm on Feb 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What happens with www and non-www versions of a domain? Is there any canonicalisation of those too?

jdMorgan

9:02 pm on Feb 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not unless you add a catch-all domain canonicalization redirect rule after the index-fixer redirect rule...

Jim