Combining two mod_rewrite rules
I suck at rewrite rules so.. pity me >_>
I want to get these two rules to work together.. basically I want to remove the www whenever its used, then send all requests to index.php with a GET variable if theres stuff after the domain..
Eg: www.example.com and www.example.com/foo would go to example.com and example.com/foo respectively.
Heres what I have to merge:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule 开发者_运维百科 (.*) index.php?token=$1 [QSA,L]
</IfModule>
I havent tested this so I donno.. but I highly doubt I have it right.
EDIT: Just to clarify, I dont mean to say that need to be one rule, but will they work together as expected?
This should be OK because your first rule is marked [L] Last and [R] Redirect - so both rules won't have to process at the same time. The first one will process, redirect, clear the first, then the second one processes.
You could use [C] to Chain together the conditions - meaning that if the first one passes, the next one is tried, but if the first one fails then all Chained subsequents are skipped. I don't think that's what you want here. If anything, you'd want an [OR] directive.
Your rewrite looks decent, except for possibly that last one, which – if you're using this for a CodeIgniter site – usually look like:
RewriteRule ^(.*)$ index.php?/$1 [L]
As a best-practice, you should setup a duplicate site at a test-subdomain.example.com so that you can fiddle with things like this in as close to a live environment as possible.
to your comment: if you want to wildcard the domains, just:
RewriteCond %{HTTP_HOST} ^www\.[^.]+\.com$ [NC]
RewriteRule ^[^.]+.([^.]+).com/(.*)$ http://$1.com/$2 [R=301,L]
Typically, a complex website can have hundreds of rules in the .htaccess
file, each with their own set of RewriteCond
. There is no need to merge them into some all-encompassing-rule.
精彩评论