2015-06-05 85 views
1

下面是我在我的htaccess现在:如何正确重定向多个URL用的.htaccess

Options +FollowSymlinks 
RewriteEngine on 
RewriteBase /themelink/mytheme 
RewriteRule ^(.+)$ http://portal.example.com/aff.php?aff=$1&p=mytheme.php [R=301,NC] 

我有这样的.htaccess地处/themelink/mytheme目录。

这样做是重定向有人去http://example.com/themelink/mytheme/123456到URL http://portal.example.com/aff.php?aff=123456&p=mytheme.php

这几乎是我想要做的,但在那里我想它但它不是相当。

我想要做的是将.htaccess放在我的themelink目录中,并让它识别URL后面的任何文件夹名称,而不必为每个主题创建单独的文件夹。

例如,这里的几集的链接,我想工作:

http://example.com/themelink/new-theme/5643434 ---> http://portal.example.com/aff.php?aff=5643434&p=new-theme.php

http://example.com/themelink/bloggertheme/254543 ---> http://portal.example.com/aff.php?aff=254543&p=bloggertheme.php

http://example.com/themelink/test-theme/4353663 --- > http://portal.example.com/aff.php?aff=4353663&p=test-theme.php

我可以在技术上做到这一点只是c为每个需要重定向设置的主题重新创建一个新目录,并仅使用上面的.htaccess,但我更愿意拥有一个仅适用于所有主题的.htaccess。

希望这是有道理的,请随时让我知道是否需要澄清。

回答

2

如果我理解正确,你可以移动的.htaccess了一个级别到/themelink目录下,并修改它包括两个()捕获组,第一组拍摄的一切行动所遇到的第一个/,第二捕捉一切之后。

# .htaccess in /themelink 
Options +FollowSymlinks 
RewriteEngine on 
# Change the RewriteBase (it actually isn't even needed) 
RewriteBase /themelink 
# $1 is the theme name, $2 is the aff value 
RewriteRule ^([^/]+)/(.+)$ http://portal.example.com/aff.php?aff=$2&p=$1.php [R=301,NC] 

表达([^/]+)一个或多个字符是一个/并捕获它作为$1匹配。

现在,我注意到所有示例中的aff值都是数字。如果是这样的话,我会建议重写一些更具体的匹配数字,而不是匹配任何东西的(.+)。这样,一个无效的URL(数字以外的东西)将不会重定向,而是可以使用404响应。

# Ensure $2 is an integer with `\d+` (one or more digits) 
RewriteRule ^([^/]+)/(\d+)$ http://portal.example.com/aff.php?aff=$2&p=$1.php [R=301,NC]