简体   繁体   中英

htaccess RewriteRule doesn't work properly

I have this code in my .htaccess file:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?link=$1 [L]
</IfModule>

And I am getting 404 although I can acces it with non-pretty url like this index.php?link=somepage

My full url is http://www.website.com/subfolder/index.php?link=somepage

I have Apache 2.4.18 on Ubuntu 16 and mod_rewrite module is loaded.

Update

Also when I try with this:

RewriteRule ^ index.php [L]

Then I can access it with http://www.website.com/subfolder/?link=somepage
So this way it works when just removing index.php

Is it possible that some additional configuration has to be set on server?

This was a server issue including mime types and multiviews.

It's not really issue for stackoverflow but if someone runs into same problems here are sources that solved this problem:

Try :

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /subfolder/index.php?link=$1 [L]

Try with this:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ ./index.php?link=$1

First of all you should check your apache config

sudo vi /etc/apache2/apache2.conf

Search for

<Directory /var/www/>
     Options Indexes FollowSymLinks
     AllowOverride None
     Require all granted
</Directory>

Change AllowOverride None to AllowOverride All

enable htaccess module by running cmd

sudo a2enmod rewrite

then restart your server

sudo service apache2 restart

and use this .htaccess file in your project folder

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]

Hope it works for u .. :)

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