使用.htaccess进行网址重定向问题

时间:2015-03-09 16:14:43

标签: php .htaccess

以下是我需要使用.htaccess转换的内容

  1. 如果url是sample.xxx.com意味着我需要将其转换为www.xxx.com/domain/sample

  2. 我的网址是sample.xxx.com/category/34/electonics.html表示我需要将其转换为www.xxx.com/domain/sample/category/34/electronics.html

  3. 3.再次转换后,我需要建立必要页面的请求

    我有以下规则

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
    RewriteCond %1 !^(www|ftp|mail)$ [NC]
    RewriteRule ^(.*)$ http://www.example.com/domain/%1/$1 [P,QSA]
    
    RewriteRule ^domain/([a-zA-Z0-9_\-]+)$ index.php?domain=$1 [Nc,L]
    RewriteRule ^domain/([a-zA-Z0-9_\-]+)/$ index.php?domain=$1 [Nc,L]
    RewriteRule ^domain/([a-zA-Z0-9_\-]+)/news/([a-zA-Z0-9_\-]+)/([0-9]+)_([0-9]+)/([a-zA-Z0-9_\-]+)\.html$ news.php?domain=$1&category=$4&news=$3 [Nc,L]
    RewriteRule ^domain/([a-zA-Z0-9_\-]+)/category/([0-9]+)/([a-zA-Z0-9_\-]+)\.html$ category.php?domain=$1&category=$2 [Nc,L]
    RewriteRule ^domain/([a-zA-Z0-9_\-]+)/gallery/([0-9]+)/([a-zA-Z0-9_\-]+)\.html$ gallery.php?domain=$1&category=$2 [Nc,L]
    RewriteRule ^domain/([a-zA-Z0-9_\-]+)/video/([0-9]+)/([a-zA-Z0-9_\-]+)\.html$ video.php?domain=$1&category=$2 [Nc,L]
    

    但它失败了

    如果我将第4行更改为

    RewriteRule ^(.*)$ http://www.example.com/domain/%1/$1 [NC,QSA]
    

    表示它可以正常工作,但浏览器会显示重建的网址。

    有人发现我的错误。

1 个答案:

答案 0 :(得分:0)

如果您将http://保留在目标URI中,那么它确实会完全重定向。

试试这个:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^(www|ftp|mail)\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule ^((?!domain/).*)$ domain/%1/$1 [L,NC]

RewriteRule ^domain/([\w-]+)/news/([\w-]+)/([0-9]+)_([0-9]+)/([\w-]+)\.html$ news.php?domain=$1&category=$4&news=$3 [NC,L]
RewriteRule ^domain/([\w-]+)/category/([0-9]+)/([\w-]+)\.html$ category.php?domain=$1&category=$2 [NC,L]
RewriteRule ^domain/([\w-]+)/gallery/([0-9]+)/([\w-]+)\.html$ gallery.php?domain=$1&category=$2 [NC,L]
RewriteRule ^domain/([\w-]+)/video/([0-9]+)/([\w-]+)\.html$ video.php?domain=$1&category=$2 [NC,L]

RewriteRule ^domain/(.+)/?$ index.php?domain=$1 [NC,L]