简体   繁体   English

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

[英]how can i control multi level subdomains with htaccess and php

I have following dynamic URL 我有以下动态网址

http://foo.mydomain.com http://foo.mydomain.com

and I have following in my .htaccess file 我的.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]

and it is working fine and showing data related to that foo subdomain. 并且工作正常,并显示与该foo子域相关的数据。 But when I try to visit 但是当我尝试访问

http://foo.mydomain.com/anything-9.html 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 其中foo是通配符DNS子域,而bla是我的帖子标题,而9是帖子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 所以我的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]

it still showing its main page ie sub.php instead of post.php What changes should I make to my .htaccess file? 它仍然显示其主页,即sub.php而不是post.php。我应该对.htaccess文件进行哪些更改?

I solved this with PHP alone. 我只用PHP解决了这个问题。 To me it's easier than Apache .htaccess scripting... Also easier to maintain (once the script is written) and has more features. 对我来说,它比Apache .htaccess脚本更容易...也更易于维护(一旦编写了脚本)并且具有更多功能。

I used an .htaccess that redirects ALL access to one file. 我使用.htaccess将所有访问重定向到一个文件。 This one uses the $_SERVER["SERVER_NAME"] variable to check the subdomain accessed, and a variable in $_GET["q"] for the rest. 这个使用$ _SERVER [“ SERVER_NAME”]变量检查访问的子域,其余使用$ _GET [“ q”]中的变量。

This is the .htaccess file: 这是.htaccess文件:

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). 然后,redir文件包含所需的任何文件,而且也很简单-$ _SERVER [“ QUERY_STRING”],$ _ GET变量设置为新值,cwdir更改工作目录(文件),set_include_path(包含路径)。

It works. 有用。 But it is kinda silly :D 但这有点愚蠢:D

您是否真的需要在此处添加2个问号:post.php ?? name =%2&post = $ 1&postid = $ 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM