Senne Vandenputte Senne Vandenputte - 4 years ago 75
PHP Question

PHP htaccess rewrite URL with parameters while keeping index page

I have been looking for a solution, but can't find one.

The problem is the following:

I have a directory on my website, for example

domain.com/directory/
. I made a custom CMS with custom URL's that are added after
directory/
, like for example
domain.com/directory/example-page
. I used htaccess to rewrite the URL of
directory/post.php?url=xxx
to
directory/xxx


The problem is that it is not working, since I have an index.php file I want to keep using.

I fixed it by doing it like this:

RewriteRule ^directory/subdirectory/(.*)$ directory/post.php?url=$1 [NC,L] [QSA]


But I don't want to use the extra subdirectory in the URL.

So, how can I rewrite
directory/post.php?url=xxx
to
directory/xxx
while keeping
directory/
working with index.php?

I know it sounds complicated, but I have been looking for a solution for days.

Thanks in advance!

Answer Source

You can use the following rule

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^directory/([^/.]+)/?$ /directory/post.php?url=$1 [QSA,L]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download