EzioPC
EzioPC

Reputation: 35

mod_rewrite redirect all requests to index.php, including directories

I want to redirect all requests to index.php.

Such as: localhost/abc/def ---> localhost/index.php?url=abc/def

Here's my .htaccess line:

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

But I have a problem, I have a directory "test", when I go to localhost/test, I want it to redirect to locahost/index.php?url=test, but the address bar reads localhost/test/?url=test.

How can I remove the query string? (?url=test or something like this when I entered address which access to directory)?

Upvotes: 2

Views: 1033

Answers (1)

anubhava
anubhava

Reputation: 785098

It is because mod_dir runs after your rewrite rule and adds a redirect to add trailing slash.

You can have this in your root .htaccess:

RewriteEngine On

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

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+?)/$ index.php?url=$1 [QSA,L]

Upvotes: 3

Related Questions