我有以下.htaccess
代码,告诉服务器将某些子域指向子文件夹,然后还处理根域的重写。
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
ErrorDocument 404 ./404.php
RewriteCond %{HTTP_HOST} ^secretplace\.testwebsite\.local
RewriteRule ^(.*)$ /secretplace/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^payment\.testwebsite\.local
RewriteRule ^(.*)$ /payment/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^login\.testwebsite\.local
RewriteRule ^(.*)$ /login/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^signup\.testwebsite\.local
RewriteRule ^(.*)$ /signup/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^fbauth\.testwebsite\.local
RewriteRule ^(.*)$ /fbauth/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^masterapi\.testwebsite\.local
RewriteRule ^(.*)$ /masterapi/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^www\.testwebsite\.local$ [NC]
RewriteRule ^(.*)$ http://testwebsite.local/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^testwebsite\.local$ [NC]
RewriteRule ^(.*)$ /account_redirect/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^testwebsite\.local$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ref_([a-zA-Z0-9-]+)/?$ index.php?ref=$1 [L,QSA]
RewriteRule ^api/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ api.php?function=$1&method=$2&extra=$3 [L,QSA]
RewriteRule ^api/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ api.php?function=$1&method=$2 [L,QSA]
RewriteRule ^api/([a-zA-Z0-9-]+)/?$ api.php?function=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ $1-$2.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L,QSA]
</IfModule>
它可以工作,但在我的本地apache安装页面加载真的很慢。只需几秒钟就可以在页面之间切换。
您知道最后一行代码允许我从页面中删除.php扩展名。因此,它不是http://testwebsite.local/features.php
,而是加载http://testwebsite.local/features/
。
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L,QSA]
我的代码中是否有明显的错误?我不太确定如何调试.htaccess。
答案 0 :(得分:1)
为什么使用P
标志而不是L
标志?这需要mod_proxy
并且可以减慢htaccess的执行速度。在我看来,你不需要它。
如果我必须自己编写代码,它看起来就像这样
ErrorDocument 404 /404.php
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(secretplace|payment|login|signup|fbauth|masterapi)\. [NC]
RewriteRule ^(.*)$ /%1/$1 [L,NC,QSA]
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^testwebsite\.local$ [NC]
RewriteRule ^(.*)$ /account_redirect/$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
RewriteRule ^ref_([a-zA-Z0-9-]+)/?$ /index.php?ref=$1 [L,QSA]
RewriteRule ^api/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ /api.php?function=$1&method=$2&extra=$3 [L,QSA]
RewriteRule ^api/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ /api.php?function=$1&method=$2 [L,QSA]
RewriteRule ^api/([a-zA-Z0-9-]+)/?$ /api.php?function=$1 [L,QSA]
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ /$1-$2.php [L,QSA]
RewriteCond %{REQUEST_URI} ^/([^/]+)/?$
RewriteRule ^.*$ /%1.php [L,QSA]