.htaccess 不确定我是否可以强制www、删除扩展、保留查询字符串并将其全部路由到index.php

.htaccess 不确定我是否可以强制www、删除扩展、保留查询字符串并将其全部路由到index.php,.htaccess,.htaccess,好吧,有一段时间我一直在使用codeigniter,但似乎从来没有人买过它。就发展而言,我不认为它会继续下去。这就是说,我终于要离开了,可以说,我想回到我的根,磨练我的一些技能。然而不幸的是,我对htaccess规则不是很在行,我被codeigniter宠坏了,用下面的几行代码为我处理它 #Removes index from url RewriteCond $1 !^(index\.php|uploads|temp_pix|assets|qrgen|robots\.txt) RewriteCo

好吧,有一段时间我一直在使用codeigniter,但似乎从来没有人买过它。就发展而言,我不认为它会继续下去。这就是说,我终于要离开了,可以说,我想回到我的根,磨练我的一些技能。然而不幸的是,我对htaccess规则不是很在行,我被codeigniter宠坏了,用下面的几行代码为我处理它

#Removes index from url
RewriteCond $1 !^(index\.php|uploads|temp_pix|assets|qrgen|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteRule ^(.*)$ /index.php?$1 [L]
我想做的是,像普通的MVC/CMS一样,通过index.php推送所有内容。 如果不是有效的文件/目录。然后我想假设它的URL中显示php/html/htm,但我不希望在URL中显示它。有时我也希望继续使用查询字符串,例如:
domain.com/something/?foo=bar&bazz=who_knows
。我还想在适用时强制使用尾部斜杠,并强制使用www


现在我已经挖掘了一点,尝试了一个接一个的组合,但我还没有找到任何真正有效的方法。htaccess应该满足您的所有要求:

# to support extension-less URIs
Options +MultiViews
RewriteEngine On

# to enforce www in domain
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

# to enforce trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} \s/+(.*?)[^/][?\s]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]

# internal rewrite to send all non files/directories to index.php as a query
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L,QSA]