.htaccess rule not working without HTTP redirect [R]

.htaccess rule not working without HTTP redirect [R] - .htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about linux, apache, htaccess, mod-rewrite, .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :


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]

Solution :

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)!)



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]

Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.

Comments

Popular posts from this blog

Rewrite in Mediawiki, remove index.php, .htaccess

.htaccess rewrite wildcard folder paths from host

Using .htaccess to set a cookie and 301 redirect