htaccess rewrite url with eventual subfolder

Viewed 47

I have urls like this:

https://mywonderfulsite.com/page

and my .htaccess rewrites it as

https://mywonderfulsite.com/index.php?source=page

The .htaccess is the following:

RewriteEngine on
RewriteBase /
RewriteRule \.(php)$ - [L]
RewriteRule ^index\.html$ - [L]
RewriteRule ^favicon\.ico$ - [L]
RewriteRule ^custom404\.html$ - [L]
RewriteRule ^custom500\.html$ - [L]
RewriteRule ^([^/]+)/?$ index.php?source=$1 [L]

This fails if the url contains a subfolder like

https://mywonderfuldomain.com/subfolder/anotherpage

that should become

https://mywonderfuldomain.com/index.php?source=/subfolder/anotherpage

I don't seem to get how to handle this situation. How to fix it?

1 Answers

Problem is with pattern ([^/]+) that matches anything but a /.

Use it like this:

RewriteEngine On

RewriteRule \.php$ - [L,NC]

RewriteRule ^(?:favicon\.ico|(?:index|custom500|custom404)\.html)$ - [L,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .+ index.php?source=$0 [L,QSA]
Related