使用 .htaccess 的 URL 重定向问题


url redirection issue using .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.再次转换这些东西后,我需要构建对必要页面的请求

我有以下规则

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]

意味着它将工作,但浏览器显示重建的 URL。

请任何人发现我的错误。

如果您将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]