我目前正在使用.htaccess在我的网站上进行重写以创建动态子域.我还使用它来删除所有页面上的.php扩展名.但是,一旦进入子域,它就会尝试再次重定向.例如,如果您转到 admin.example/test ,则实际上是访问 example/clients/admin/test.php .我使用以下.htaccess文件不断收到各种404错误:
I'm currently using .htaccess to rewrite on my website to create dynamic subdomains. I also use it to remove the .php extension on all pages. However, once inside the subdomain, it tries to redirect again. For example, if you went to admin.example/test, it would actually be accessing example/clients/admin/test.php. I keeping getting various 404 errors using the following .htaccess file:
Options +MultiViews Options +FollowSymLinks RewriteEngine On RewriteRule ^subdomains/(.*)/(.*) $1.example/$2 [r=301,nc] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.php [NC,L] RewriteCond %{HTTP_HOST} !^(www\.)?example\$ [NC] RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\$ [NC] RewriteCond %{DOCUMENT_ROOT}/%2%{REQUEST_URI}/ -d RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L] RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteCond %{HTTP_HOST} !^(www\.)?example\$ [NC] RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\$ [NC] RewriteRule ^(.*)$ clients/%2/$1 [QSA,L]如何防止重定向到 admin.example/clients/admin/test.php ?
推荐答案首先,您可能想关闭多视图,这将使整个删除php扩展名"事情变得混乱
First, you probably want to turn off multiviews, that's going to mess with the whole "removing the php extension" thing
然后,您需要以下规则:
Than, you want this rule:
RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.php [NC,L]要在进行重定向之后,并且想要对实际的重定向进行重定向状态检查:
To be after your redirects, and you want to be doing the redirect status check on the actual redirects:
Options -Multiviews + FollowSymLinks RewriteEngine On RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteRule ^subdomains/(.*)/(.*) $1.example/$2 [r=301,nc] RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteCond %{HTTP_HOST} !^(www\.)?example\$ [NC] RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\$ [NC] RewriteCond %{DOCUMENT_ROOT}/%2%{REQUEST_URI}/ -d RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L] RewriteCond %{REQUEST_URI} !^/clients/ RewriteCond %{HTTP_HOST} !^(www\.)?example\$ [NC] RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\$ [NC] RewriteRule ^(.*)$ clients/%2/$1 [QSA,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^([^\.]+)$ $1.php [NC,L]更多推荐
使用.htaccess重写动态子域
发布评论