Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I am using the following .htaccess with Wordpress to remove index.php from the visible URL. This works but forces an http redirect so the URL changes in the browser window. If I remove the [R] it no longer works. Why would this happen?

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [R] 
share|improve this question
    
Could you clarify the 'it no longer works' part? Are you getting 404 errors? –  AVee Jan 12 '12 at 14:44
    
Sorry no it is going to the index.php. It seems like the query string is getting dropped or something. I want urls like domain.com/products/rental-options/ to redirect to domain.com/index.php/products/rental-options/ without changing the URL in the address bar –  Michael Cohen Jan 12 '12 at 14:57
    
Have you had a look at the RewriteLog? I've found it's extremely helpful with a high RewriteLogLevel. Note this needs to be set in your server config, not in .htaccess. –  Pumbaa80 Jan 12 '12 at 15:15
    
It's a shared hosting account so I'm sticking with .htaccess. My hands are tied with most server config files. –  Michael Cohen Jan 12 '12 at 15:22
1  
Just use the default htaccess wordpress provides. –  Gerben Jan 12 '12 at 17:04
show 1 more comment

migrated from stackoverflow.com Jan 18 '12 at 3:03

This question came from our site for professional and enthusiast programmers.

2 Answers

It seems you're mixing Wordpress itself, which tries to do a redirect, and the RewriteRule, where you don't have to do a redirect: it's rewritten internally before arriving to Php, thus before arriving to Wordpress. If you just stop rewriting and add the QSA flag to always keep the query string, this should do the trick.

Try to do this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [QSA,L]

If it doesn't work, try to do this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php/$1 [QSA,L]

If it doesn't work, try to do this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !(index\.php)
RewriteRule ^(.*)$ /index.php/$1 [QSA,L]

And if that's not enough:

Two hints:

If you're not in a hosted environment (= if it's your own server and you can modify the virtual hosts, not only the .htaccess files), try to use the RewriteLog directive: it helps you to track down such problems:

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

My favorite tool to check for regexp:

http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

share|improve this answer
    
Unfortunately that did not work and this is shared hosting so no RewriteLog –  Michael Cohen Jan 17 '12 at 19:29
    
Answer updated, tell me if it worked ;) –  Olivier Pons Jan 17 '12 at 20:54
add comment
up vote 1 down vote accepted

It appears that I forgot a major piece. I was skipping over the fact that the query string needed to be sent to the get variable q.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.