【问题标题】:How to rewrite a subdomain with .htaccess?如何用 .htaccess 重写子域?
【发布时间】:2014-02-15 05:37:35
【问题描述】:

我有一个这样的子域:

Example: http://us.example.com/index.php?city=newyork

我该如何重写:

http://us.example.com/newyork

“us”是一个虚拟子域。所以,它可以是不同的:我们、fr、it 等等。

我用这种方法试过了,但是不行

RewriteEngine on

RewriteCond %{HTTP_HOST} !^example\.com
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*) index.php?city=$1

【问题讨论】:

标签: php .htaccess mod-rewrite subdomain


【解决方案1】:

保持这样:

RewriteEngine on

# external redirect from actual URL to pretty one
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.example\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/+index\.php\?city=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.example\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?city=$1 [L,QSA]

编辑:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.example\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/+index\.php\?city=([^&]+)&ountry=([^\s&]*)\s [NC]
RewriteRule ^ /%1/%2? [R=302,L]

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.example\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?city=$1&country=$2 [L,QSA]

【讨论】:

  • 太棒了!太完美了!如果我想添加另一个参数,例如:从us.example.com/index.php?city=newyork&country=usaus.example.com/newyork/usa 我该怎么办?
  • 非常感谢阿努巴瓦!但我忘了告诉你,第二个参数一定不是强制性的。我没有话要感谢你!
  • 喜欢这个工作:us.example.com/newyork/usa 而不是喜欢这个不起作用:us.example.com/newyork 这种情况下的第二个参数“usa”必须是可选的
  • 应该可以,您是否在 .htaccess 中结合了这两组规则?
  • 对不起anubhava,我犯了一个错误:-) ..一切都很好!非常感谢!!
猜你喜欢
  • 1970-01-01
  • 2011-05-15
  • 2011-02-04
  • 2014-03-17
  • 1970-01-01
  • 1970-01-01
  • 2021-12-10
  • 2010-12-29
  • 2011-05-25
相关资源
最近更新 更多