Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
946 views
in Technique[技术] by (71.8m points)

regex - Apache Multiple Rewrite Rules

I have a 2 sets of rewrite rules. This is the 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 is the .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 have been trying to add the .htaccess to the Virtual Host so I can remove the .htaccess file - below is an example, but I get the 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 the [L] means last rule to match so I have removed that but it still doesn't work.

What am I missing here? I've tried reversing the rules.

thankyou

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

L will still be needed as Last flag is for marking end of each rewrite rule. Ordering of 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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.7k users

...