使用.htaccess重写动态子域

编程入门 行业动态 更新时间:2024-10-19 05:30:40
本文介绍了使用.htaccess重写动态子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在使用.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重写动态子域

本文发布于:2023-10-31 16:03:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546581.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重写   动态   htaccess

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!