Domain name in URL as variable

被刻印的时光 ゝ 提交于 2020-01-03 05:38:08

问题


My .htaccess file has the following code:

RewriteEngine on
RewriteRule ^/?(.*)$ index.php?domain=$1 [L]

I'm trying to get domain names as variables from URLs like:

hxxp://www.example.com/www.domain.name or

hxxp://www.example.com/subdomain.domain.name or

hxxp://www.example.com/domain.name

but with $_GET['domain'] my variable is always 'index.php' and not the domain names.

With hxxp://www.example.com/domain/www.domain.name and .htaccess code

RewriteEngine on
RewriteRule ^domain/?(.*)$ index.php?url=$1 [L]

everything is OK, but I would like to remove the 'domain/' part from the URLs.

I've searched for this, but couldn't find anything. Could someone please help me with this?


回答1:


something like

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?domain=$1 [L]

in this case $1 will be:

http://site/www.example.com        $1 = www.example.com
http://site/www.example.com/xyz    $1 = www.example.com/xyz



回答2:


Try this rule:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/.](\.[^/]+)+$ index.php?domain=$0 [L]

This will rewrite any request with a URL path that contains at least one dot (foo.bar, foo.bar.baz, etc.) to your index.php.



来源:https://stackoverflow.com/questions/2039801/domain-name-in-url-as-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!