Home > database >  Apache2 .htaccess rewrite rule for nested URLs doesn't work
Apache2 .htaccess rewrite rule for nested URLs doesn't work

Time:03-13

I have simple php application with navigation based on domain/foo/bar nested urls.

For instance, I have main page index.php with about nav link which should navigate to domain/en/about, where en and about must be transfered to url param like index.php?url=....

But when I click to about I got to domain/en/aboutand 404 not found instead.

I have configured apache2 virtual domain config as:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    <Directory /var/www/html/domain>
         Options -Indexes  FollowSymLinks -MultiViews
         AllowOverride All
         Require all granted
     </Directory>
    DocumentRoot /var/www/domain/   
    ServerName domain.local
    ServerAlias www.domain.local
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

And .htaccess file as:

order deny,allow
RewriteEngine On
RewriteBase /
RewriteRule .* index.php?url=$0 [QSA,L]

mod_rewrite for apache2 is already enabled.

Have no clue what I have missed.

Any help is appreciated! Thank you in advance!

CodePudding user response:

<Directory /var/www/html/domain>
:
DocumentRoot /var/www/domain/

Your <Directory> section and DocumentRoot directive refer to different locations, so regardless of where you've put the .htaccess file, it's not going to work as intended.

However...

RewriteRule .* index.php?url=$0 [QSA,L]

This will result in an endless rewrite-loop since it will rewrite itself, ie. index.php?url=index.php etc.

You need to prevent requests to index.php itself being rewritten, which you can do by adding an additional rule. For example:

RewriteRule ^index\.php$ - [L]
RewriteRule .* index.php?url=$0 [QSA,L]

However, this will still rewrite your static assets (assuming you are linking to internal images, CSS and JS files?). So, you would normally need to prevent this with an additional conditon that prevents the rule from being processed if the request already maps to a static file.

For example:

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php?url=$0 [QSA,L]

The CondPattern -f checks if the TestString maps to a file. The ! prefix negates this. So the condition is only successful when the request does not map to a file.

CodePudding user response:

You need parenthesis around what you want to capture. Back-references indices start with '1':

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