Sarfo
Sarfo

Reputation: 117

htaccess routing that affects specific directory

I want to place an .htaccess file into a specific directory (e.g. /mydir) so that instead of writing an URL like this:

www.example.com/mydir/file.php?x=hey&y=ben you can write this:
www.example.com/mydir/hey/ben.

The routing must affect only the URLs that start with www.example.com/mydir while being able at the same time to write URLs like this: www.example.com/anotherdir/test.php.

I tried below rules to apply this to all URLs (I've put that in the main root)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L] 

but I don't know how to make it for a sub-directory.

Upvotes: 1

Views: 45

Answers (1)

wp78de
wp78de

Reputation: 18980

If there is no .htaccess in /mydir/, you can use this first rule in your **root**.htaccess`:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^x=([^&]+)&y=([^&]+) [NC]
RewriteRule ^mydir/file\.php$ /mydir/%1/%2 [NC,L,R=302,QSD]

Try it in a fresh browser in incognito mode.

Upvotes: 1

Related Questions