Luv2Muff
asked on
.htaccess rewrite query
I currently use the following as standard on all the sites I develop:
# Replace mydomain/index.php with /
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php [NC]
RewriteRule . / [R=301,L]
# Force Trailing Slash For Folders
RewriteCond %{REQUEST_URI} ^/[^.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
# Remove all instances of index.php leaving just the preceding /
# RewriteRule (.*)index\.php(.*) $1$2 [R,L]
The problem is the last one not only replaces index.php with a / it also replaces stimgs such as index.php?pageId=25
How can I get index.php replaced with a / only if there is not a string after it?
Hope this makes sense.
Thanks,
Luv2.
# Replace mydomain/index.php with /
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php [NC]
RewriteRule . / [R=301,L]
# Force Trailing Slash For Folders
RewriteCond %{REQUEST_URI} ^/[^.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
# Remove all instances of index.php leaving just the preceding /
# RewriteRule (.*)index\.php(.*) $1$2 [R,L]
The problem is the last one not only replaces index.php with a / it also replaces stimgs such as index.php?pageId=25
How can I get index.php replaced with a / only if there is not a string after it?
Hope this makes sense.
Thanks,
Luv2.
ASKER
Glad to see you are on the case revenpl.
I have tried this as you suggested, but for example:
http://www.doublespark.co.uk/website-design/index.php
does not get re-written as :
http://www.doublespark.co.uk/website-design/
Can you have another look?
Thanks,
Luv2.
I have tried this as you suggested, but for example:
http://www.doublespark.co.uk/website-design/index.php
does not get re-written as :
http://www.doublespark.co.uk/website-design/
Can you have another look?
Thanks,
Luv2.
And where it's redirected? To http://www.doublespark.co.uk/ ? If so, then the first rule fired first
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php [NC] # match for website-design/index.php
RewriteRule . / [R=301,L]
IMHO since the last one is about to remove any trailing index.php, You should disable the first rule.
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php [NC] # match for website-design/index.php
RewriteRule . / [R=301,L]
IMHO since the last one is about to remove any trailing index.php, You should disable the first rule.
ASKER
Sorry ravenpl, I think I am not being clear and that I am confusing you.
What I want is any url that ends with /index.php being we-witten just leaving the /
So that
http://www.doublespark.co.uk/website-design/index.php
would become:
http://www.doublespark.co.uk/website-design/
and
http://www.doublespark.co.uk/index.php
would become:
http://www.doublespark.co.uk/
But
http://www.doublespark.co.uk//index.php?1234
would be not be re-written
Sorry for not explaining myself very well in the first place.
What I want is any url that ends with /index.php being we-witten just leaving the /
So that
http://www.doublespark.co.uk/website-design/index.php
would become:
http://www.doublespark.co.uk/website-design/
and
http://www.doublespark.co.uk/index.php
would become:
http://www.doublespark.co.uk/
But
http://www.doublespark.co.uk//index.php?1234
would be not be re-written
Sorry for not explaining myself very well in the first place.
ASKER
I guess this explains what I need but in one line:
#Remove all instances of index.php leaving just the preceding / but only if query string is empty
#Remove all instances of index.php leaving just the preceding / but only if query string is empty
OK, I would go this way, I don't know why You have the first rule using THE_REQUEST
# Force Trailing Slash For Folders access
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} ![^/]$
RewriteRule (.*) $1/ [R,L]
#bit isn't it better to load mod_dir? http://httpd.apache.org/docs/2.0/mod/mod_dir.html#directoryslash
#remove index.php if no query string after that
RewriteCond ${QUERY_STRING} ^$
RewriteRule (.*)index\.php$ $1 [R,L]
# Force Trailing Slash For Folders access
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} ![^/]$
RewriteRule (.*) $1/ [R,L]
#bit isn't it better to load mod_dir? http://httpd.apache.org/docs/2.0/mod/mod_dir.html#directoryslash
#remove index.php if no query string after that
RewriteCond ${QUERY_STRING} ^$
RewriteRule (.*)index\.php$ $1 [R,L]
ASKER
Thanks for trying, but index.php is never removed
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect - many thanks!!
ASKER
Perfect!!
I still wonder why it will not work for me - I was debugging it for hour!
But glad it works for You.
But glad it works for You.
ASKER
I do appreciate the time you put in to this, I have been wanting that bit of code for a long time.
Thanks again,
Luv2.
Thanks again,
Luv2.
RewriteCond ${QUERY_STRING} ^$
RewriteRule (.*)index\.php$ $1 [R,L]