Solved

.htaccess rewrite query

Posted on 2007-11-24
12
855 Views
Last Modified: 2010-04-21
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.
0
Comment
Question by:Luv2Muff
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 43

Expert Comment

by:ravenpl
ID: 20343777
#only if query string is empty
RewriteCond ${QUERY_STRING} ^$
RewriteRule (.*)index\.php$ $1 [R,L]
0
 

Author Comment

by:Luv2Muff
ID: 20343939
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.
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 20344072
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.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:Luv2Muff
ID: 20344109
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.
0
 

Author Comment

by:Luv2Muff
ID: 20344113
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
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 20345072
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]
0
 

Author Comment

by:Luv2Muff
ID: 20345206
Thanks for trying, but index.php is never removed
0
 
LVL 43

Accepted Solution

by:
ravenpl earned 500 total points
ID: 20346630
The above second rule have obvious bug
RewriteCond %{QUERY_STRING} ^$
RewriteRule (.*)index\.php$ $1 [R=301,L]

and it should work - hence does not for me, I wonder why?
0
 

Author Comment

by:Luv2Muff
ID: 20346668
Perfect - many thanks!!
0
 

Author Closing Comment

by:Luv2Muff
ID: 31410766
Perfect!!
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 20346689
I still wonder why it will not work for me - I was debugging it for hour!
But glad it works for You.
0
 

Author Comment

by:Luv2Muff
ID: 20346840
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.
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As Wikipedia explains 'robots.txt' as -- the robot exclusion standard, also known as the Robots Exclusion Protocol or robots.txt protocol, is a convention to prevent cooperating web spiders and other web robots from accessing all or part of a websit…
If you are running a LAMP infrastructure, this little code snippet is very helpful if you are serving lots of HTML, JavaScript and CSS-related information. The mod_deflate module, which is part of the Apache 2.2 application, provides the DEFLATE…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question