简体   繁体   中英

.htaccess RewriteRule Not Working with Apache2

I know there are a million posts about this, but I have read through a lot of them and made so many changes to get this to work, but I am at a loss. I have setup a redirect rule in my apache2 server web server, but it does not redirect.

My web server root folder structure:

/var/www/example/
|
|----l
     |
     |---index.php
     |---.htaccess

/var/www/example/l/.htaccess content is:

$ cat /var/www/example/l/.htaccess 

RewriteEngine On 
RewriteRule ^l/([a-zA-Z0-9]{6})$ l/index.php?redirect=$1 [R=302,L]

I have also tried putting it in the root folder /var/www/example/.htaccess

My rewrite rule appears to be working correctly from this site: https://htaccess.madewithlove.be?share=6cae684f-740d-4add-b711-53cdb5986681

mod_rewrite.so is installed and rewrite_module is loaded.

I ran sudo a2enmod rewrite and it was turned on

APache2.conf:

$ cat /etc/apache2/apache2.conf
<Directory "/var/www/example">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

When ever I go to https://example.com/l/i4Ikn2 I just get a 404 error.

I just want to know if I am missing something or somewhere to look. I checked the apache2 error.log and there are no error in there pertaining to this.

As @mrwhite and @anubhava mentioned in the comments below my OP, I was mixing up the way my .htaccess file was being used.

I removed the sub-directory flags (the l in the pattern) and added the QSA instead of the redirect ( R=302 ). This was because I was using the .htaccess file in the sub-directory. The QSA was the correct 'option' for the redirect/rewrite (still unsure of the difference).

Original:

RewriteRule ^l/([a-zA-Z0-9]{6})$ l/index.php?redirect=$1 [R=302,L]

New (working):

RewriteRule ^([a-zA-Z0-9]{6})$ index.php?redirect=$1 [QSA,L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM