tone7
tone7

Reputation: 355

Simple mod_rewrite issue

I have a real weird issue

I'm playing around with .htaccess and trying to redirect all requests to the /test/ folder's index file.

My site lies in a folder /test/ in my local htdocs folder. No other files exist currenlty.

What I expect: When I visit any url, (for example /test/category/one/) I should be redirected to /test/index.php

What happens I get a 404 Not Found

My .htaccess looks like this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /test/index.php?__route=$1 [L,QSA]

I have tried setting RewriteBase /test/

This is as straight forward as it gets so why isn't it working?

I have a Wordpress site in another folder and that works flawlessly with custom rewrites.

I even copied the Wordpress' .htaccess contents to the test site's, substituting the rewrite base and last rule with /test/.

Wordpress' .htaccess: (which works on a seperate WP install on same server)

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /test/index.php [L]
</IfModule>

# END WordPress

I have been struggling with this for a while now and read quite a few SO articles with no help.

I even write a rewrite log file and now there shows nothing when I browse to the test site but a visit to the Wordpress site writes quite a few lines.

I am running XAMPP on a Win64 machine.

Any help would be greatly appreciated! =)

Upvotes: 2

Views: 3285

Answers (2)

Jonah Bishop
Jonah Bishop

Reputation: 12581

Update: Also, make sure the line endings in your .htaccess file are set appropriately. Apache can sometimes choke on anything that doesn't include the new-line (\n) character.

So it looks to me like you want to (at some level) emulate what WordPress is doing. Here's how I handled this case when I was developing some software that did the same thing:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteRule ^.*$ /index.php [L]
</IfModule>

For files that exist (i.e. either the -f or -d test passes), we serve them up unchanged. Otherwise, we redirect incoming requests to index.php. Note that the /test portion of the path is not included in the RewriteRule, since the RewriteBase set up where we were starting from. So, in your example, I think it would end up being:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteRule ^(.*)$ /index.php?__route=$1 [L,QSA]
</IfModule>

FWIW, I'm no .htaccess expert. I've simply found this to work for me in the past.

Also, if you're on a shared host (like DreamHost), you may need to set up the appropriate rules for allowing default error documents. Some shared web hosts serve up a single file (failed_auth.html is one example) for error cases. If you're not filtering out that case, you may end up with a 404.

Upvotes: 5

Joshua Burns
Joshua Burns

Reputation: 8572

This should do the trick:

# Activate the rewrite module.
RewriteEngine On
# Ensure the requested URL is not a file.
RewriteCond %{REQUEST_FILENAME} !-f
# Ensure the requested URL is not a directory.
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?__route=$1 [L,QSA]

Upvotes: 2

Related Questions