Forum Moderators: phranque

Message Too Old, No Replies

Redirect index.php to / and querystring

htaccess redirect

         

Boxer

5:13 am on Oct 11, 2010 (gmt 0)

10+ Year Member



Hello,

I hope that you can help me with my .htaccess file
Here is what I need:

Redirect all index.php files to / (they are in different folders)
Redirect all index.php querystrings (index.php?89 and /?89 to /)
Exclude 1 folder from this

Hope somebody can help me with this

sublime1

3:44 am on Oct 12, 2010 (gmt 0)

10+ Year Member



Hi Boxer!

Welcome to WebmasterWorld, and yes, someone can help you!

But please take a moment check out the charter of the forum, linked right near the top. There's a higher standard of diligence requested of people asking questions than in many places on the web -- if you can describe what you have done, provide examples, indicate what does and doesn't work, etc. then the good folks here will happily assist, in hopes that both the requester and future readers can learn.

Thanks!

Tom

Boxer

5:04 am on Oct 12, 2010 (gmt 0)

10+ Year Member



Hi Tom,

I have been googling this and found a little here and a little there.
It seems to be working, but I am not sure the code I am using is correct:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.info [nc]
RewriteRule (.*) http://www.domain.info/$1 [R=301,L]

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

RewriteCond $1 !^forum/adm/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*(index\.php)?\?[^\ ]*\ HTTP/
RewriteRule ^(([^/]+/)*)(index\.php)?$ http://www.domain.info/$1? [R=301,L]

sublime1

2:19 pm on Oct 12, 2010 (gmt 0)

10+ Year Member



I'll add comments to the code

# return an external permanent redirect to the www subdomain if a request is made without the www
# This "canonicalizes" your domain, which is good
RewriteCond %{HTTP_HOST} ^domain\.info [nc]
RewriteRule (.*) http://www.domain.info/$1 [R=301,L]

# If the full orignal request starts with from 3 to 9 upper-case letters, followed by a space and /, then
# anything ending in index.php, another space and then the characters HTTP/. This is probably intended to match
# something like "GET /index.php HTTP/1.1", but the .* would also make it match "GET /windex.php HTTP/1.1", I think
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
# if the above test passes, take any path ending in index.php and send a permanent redirect to
# a / plus whatever is before the index.php -- the $1 picks up the things in parens (.*)
# Again, I doubt this is what you want
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]

# I think this is just wrong, but this could be something I don't know about. $1 is usually a reference to the first thing
# that is enclosed in parenthesis, so not sure why it's here, and the second part says "does not match something starting
# with "forum/adm/"
RewriteCond $1 !^forum/adm/
# Similar to the rewrite cond above, but this one doesn't limit the number of characters the request starts with, just saying
# 1 or more capital letters, a space, a slash, zero or more occurrences of anything except a / but ending with a /, and then
# a possible, but not required "index.php", then an actual "?", anything except a /, a space, and HTTP/. Phew!
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*(index\.php)?\?[^\ ]*\ HTTP/
# Yikes. Something similar to the above for a match on the path, should be redirected to the domain name plus everything that
# matches (([^/]+/)*).
RewriteRule ^(([^/]+/)*)(index\.php)?$ http://www.domain.info/$1? [R=301,L]


I think these conditions are unrelated to the problem you described in the first post. Start simple, and you have to be able to do some testing so you know what does and doesn't work. (Just make a copy of the current .htaccess first so after your test you can revert back to a known working copy).

Some examples of exactly what you're looking for (replace your real domain with example.com), and what happened when you tried would be very helpful.

Tom

jdMorgan

3:00 pm on Oct 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Rule order is incorrect, and several other details need fixing-up:

RewriteEngine on
#
# Externally redirect requests for "/" or "/index.php" plus query string in any directory
# to "/" in the originally-requested directory, removing the query string. All requests to
# the /forum/adm subdirectory are excluded from this rule, and will not be redirected.
RewriteCond $1 !^forum/adm/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*(index\.php)?\?[^\ ]*\ HTTP/
RewriteRule ^(([^/]+/)*)(index\.php)?$ http://www.domain.info/$1? [R=301,L]
#
# Externally redirect all requests for "/index.php" without query string
# in any directory to "/" in the originally-requested directory.
# Note that /forum/adm subdirectory is NOT excluded from this rule.
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://www.domain.info/$1 [R=301,L]
#
# Externally redirect requests for non-canonical domain.info and www.domain.info
# hostname variants to canonical hostname, to include non-www, appended FQDN
# indicator or port number, or uppercase characters in requested hostname.
RewriteCond %{HTTP_HOST} ^domain\.info [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.info(\.|\.?:[0-9]+)$ [NC,OR]
RewriteCond %{HTTP_HOST} [A-Z]
RewriteRule ^(.*)$ http://www.domain.info/$1 [R=301,L]

If you do not use or intend to use any subdomains except "www," then consider using this much-shorter alternative for the final rule:

# Externally redirect requests for all non-blank non-canonical hostnames to the canonical hostname
RewriteCond %{HTTP_HOST} !^(www\.domain\.info)?$
RewriteRule ^(.*)$ http://www.domain.info/$1 [R=301,L]

Jim

Boxer

4:35 pm on Oct 12, 2010 (gmt 0)

10+ Year Member



Thank you jdMorgan, it is working fine.