域名正则表达式为.htaccess

时间:2012-08-03 11:06:31

标签: .htaccess url-rewriting

首先,抱歉我的英语不好。 我想配置我的.htaccess来重写URL。

example.com/company1.com

而不是example.com/sub=company1.com

我的.htaccess现在:

RewriteEngine On
RewriteRule ^([a-z_]+)/?$ index.php?sub=$1

我在stackoverflow中搜索。 如果我对所有字符使用(。*)正则表达式或([a-z \。] +)在域字符串(company1.con)中包含“点”字符,我的皮肤就被打破了。 我的临时解决方案是使用([a-z _] +)代替http://example.com/company1_com http://example.com/company1.com 这是不好的解决方案:( 所以,请给我这个问题的正则表达式。 感谢。

3 个答案:

答案 0 :(得分:1)

mod_rewrite中描述了Apache的重写。

对您而言,只要忽略可能的GET参数或路径,就应该

RewriteEngine On
RewriteRule ^/([^?/]+) /index.php?sub=$1 [L]

我想它已被破坏,因为你在index.php之前缺少“/”,GET中有一条更长的路径(example.com/company1.com/css/style.css)或者你提交了一个表格( example.com/company1.com?a=foo&b=bar)。

答案 1 :(得分:0)

试试这个

RewriteEngine On
RewriteRule ^(.*)$ index.php?sub=$1

答案 2 :(得分:0)

您需要阻止index.php循环播放:

RewriteEngine On
# let index.php pass through, thus stopping the rewrite loop
RewriteRule index.php - [L]
# route everything to index.php
RewriteRule ^(.*)$ /index.php?sub=$1 [L]

您还可以先检查现有资源。由于index.php存在,这也会破坏循环。如果您要求静态内容(如javascript或css),它将无法通过index.php进行路由:

RewriteEngine On
# request isn't for an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# route everything to index.php
RewriteRule ^(.*)$ /index.php?sub=$1 [L]