Multiple Apache rewrite rules

I have 2 sets of rewrite rules. This is a virtual host:

<VirtualHost *:80> ServerName datingjapan.co ServerAlias *.datingjapan.co RewriteEngine on RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^(.*) http://www.%{HTTP_HOST}$1 [R=301,L] DocumentRoot /var/www/html/datingjapan.co </VirtualHost> 

and this .htacess

 <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-l RewriteRule ^(.*)$ index.php?/$1 [L] </IfModule> 

I am trying to add .htaccess to a virtual host so that I can delete the .htaccess file - below is an example, but I get a site to show:

 <VirtualHost *:80> ServerName datingjapan.co ServerAlias *.datingjapan.co RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-l RewriteRule ^(.*)$ index.php?/$1 RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^(.*) http://www.%{HTTP_HOST}$1 [R=301,L] DocumentRoot /var/www/html/datingjapan.co </VirtualHost> 

I understand that [L] means the last rule matches, so I deleted it, but it still does not work.

What am I missing here? I tried to change the rules.

Thankyou

+4
source share
1 answer

L will continue to be necessary since the last flag is intended to indicate the end of each rewrite rule. The order of the rules is also important. Change your code to this:

 <VirtualHost *:80> ServerName datingjapan.co ServerAlias *.datingjapan.co DocumentRoot /var/www/html/datingjapan.co RewriteEngine on RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^(.*) http://www.%{HTTP_HOST}$1 [R=301,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-l RewriteRule ^(.*)$ /index.php?/$1 [L,QSA] </VirtualHost> 
+9
source

Source: https://habr.com/ru/post/1486559/


All Articles