如何处理www.username.domain.net

时间:2016-11-19 21:41:43

标签: regex apache .htaccess drupal url-rewriting

我将旧网站从自定义php转换为drupal,但我很难获得一些URL重定向工作。旧网站使用了" 用户名 .domain.net"等网址,但在drupal下,我必须使用类似" www.domain.net的网址结构/简档/ 用户名" (" 用户名"是会员姓名的占位符。)

我有" 用户名 .domain.net"重定向到" www.domain.net/profile/ 用户名"很好,但正则表达式无法处理像" www。用户名 .domain.net" (导致大量重复内容)。

在我的.htaccess文件中:

# redirect old user subdomains (user.domain.net > www.domain.net/profile/user)
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.net [NC]
RewriteRule (.*) http://www.domain.net/profile/%1/ [R=301,L]

# To redirect all users to access the site WITH the 'www.' prefix,
RewriteCond %{HTTP_HOST} ^domain\.net$ [NC]
RewriteRule ^(.*)$ http://www.domain.net/$1 [L,R=301]

我需要一些帮助来弄清楚如何处理具有" www"的网址的重定向?和" 用户名"前缀(即www. 用户名 .domain.com),因此它可以正确地重定向到www.domain.net/profile/ 用户名

1 个答案:

答案 0 :(得分:0)

您可以使用:

# redirect old user subdomains ((www.)user.domain.net > www.domain.net/profile/user)
RewriteCond %{HTTP_HOST} ^(?:www\.)?((?!www).*)\.domain\.net [NC]
RewriteRule ^ http://www.domain.net/profile/%1%{REQUEST_URI} [NE,R=301,L]

# To redirect all users to access the site WITH the 'www.' prefix,
RewriteCond %{HTTP_HOST} ^domain\.net$ [NC]
RewriteRule ^ http://www.domain.net%{REQUEST_URI} [NE,R=301,L]