简体   繁体   中英

how can i control multi level subdomains with htaccess and php

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 ie sub.php instead of post.php What changes should I make to my .htaccess file?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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