我如何使用 htaccess 和 php 控制多级子域

发布于 2024-10-01 07:49:30 字数 1245 浏览 0 评论 0原文

我有以下动态 URL

http://foo.mydomain.com

并且我的 .htaccess 文件中有以下

RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.domain\.com$ [NC]
RewriteRule !^index\.php($|/) sub.php?name=%2 [PT,L]

内容工作正常并显示与该 foo 子域相关的数据。但是当我尝试访问

http://foo.mydomain.com/anything-9.html

其中 foo 是通配符 dns 子域,bla 是我的帖子标题,9 是

我添加的

RewriteRule ^/([^/]+)-([^/]+)\.html$ post.php?name=%2&post=$1&postid=$2 [PT,L]

帖子 id,因此

RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.domain\.com$ [NC]
RewriteRule !^index\.php($|/) sub.php?name=%2 [PT,L]

我的 htaccess 代码

RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.domain\.com$ [NC]
RewriteRule !^index\.php($|/) sub.php?name=%2 [PT,L]
RewriteRule ^/([^/]+)-([^/]+)\.html$ post.php?name=%2&post=$1&postid=$2 [PT,L]

仍然显示其主页,即 sub.php 而不是 post.php 我应该对 .htaccess 文件进行哪些更改?

I have following dynamic URL

http://foo.mydomain.com

and I have following in my .htaccess file

RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.domain\.com$ [NC]
RewriteRule !^index\.php($|/) sub.php?name=%2 [PT,L]

and it is working fine and showing data related to that foo subdomain. But when I try to visit

http://foo.mydomain.com/anything-9.html

where foo is a wildcard dns subdomain and bla is my post title and 9 is post id

i have added

RewriteRule ^/([^/]+)-([^/]+)\.html$ post.php?name=%2&post=$1&postid=$2 [PT,L]

after

RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.domain\.com$ [NC]
RewriteRule !^index\.php($|/) sub.php?name=%2 [PT,L]

so my htaccess code becomes

RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.domain\.com$ [NC]
RewriteRule !^index\.php($|/) sub.php?name=%2 [PT,L]
RewriteRule ^/([^/]+)-([^/]+)\.html$ post.php?name=%2&post=$1&postid=$2 [PT,L]

it still showing its main page i.e sub.php instead of post.php
What changes should I make to my .htaccess file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌上青苔 2024-10-08 07:49:30

我只用 PHP 解决了这个问题。对我来说,它比 Apache .htaccess 脚本编写更容易...也更易于维护(一旦编写了脚本)并且具有更多功能。

我使用了 .htaccess 将所有访问重定向到一个文件。这个使用 $_SERVER["SERVER_NAME"] 变量来检查访问的子域,并使用 $_GET["q"] 中的变量来检查其余部分。

这是 .htaccess 文件:

RewriteEngine on
RewriteRule ^redir.php$ redir.php [L,QSA]
RewriteRule ^(.*)$ redir.php?q=$0 [L,QSA]

第一个文件确保没有无限循环,第二个文件将所有内容重定向到该文件。

然后 redir 文件包含所需的任何文件,而且它非常简单 - $_SERVER["QUERY_STRING"]、$_GET 变量设置为新值,cwdir 更改工作目录(文件),set_include_path(包含路径)。

有用。但这有点傻:D

I solved this with PHP alone. To me it's easier than Apache .htaccess scripting... Also easier to maintain (once the script is written) and has more features.

I used an .htaccess that redirects ALL access to one file. This one uses the $_SERVER["SERVER_NAME"] variable to check the subdomain accessed, and a variable in $_GET["q"] for the rest.

This is the .htaccess file:

RewriteEngine on
RewriteRule ^redir.php$ redir.php [L,QSA]
RewriteRule ^(.*)$ redir.php?q=$0 [L,QSA]

First one makes sure there is no infinite loop, second one redirects everything to the file.

The redir file then includes whatever file needed, and also it is quite seemless - the $_SERVER["QUERY_STRING"], $_GET variables are set to new values, cwdir to change the working directory (files), set_include_path (include paths).

It works. But it is kinda silly :D

故事和酒 2024-10-08 07:49:30

你真的需要这里有2个问号吗:post.php??name=%2&post=$1&postid=$2

Do you really need 2 question marks here: post.php??name=%2&post=$1&postid=$2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文