.htaccess条件语句

时间:2013-03-04 12:09:33

标签: .htaccess seo subdomain wildcard-subdomain

我想使用通配符子域将我的网址重写为更多seo网址,我无法编写htaccess条件并希望得到一些帮助。

我有这个网址:

  

http://learntipsandtricks.com/blog/c/magento

我想改写为:

  

http://magento.learntipsandtricks.com/

更改分页链接:

  

http://learntipsandtricks.com/blog/92/magento/3

到:

  

http://magento.learntipsandtricks.com/p-92-3

最后更改文章网址:

  

http://learntipsandtricks.com/blog/magento/114/magento-index-management-Cannot-initialize-the-indexer-process

为:

  

http://magento.learntipsandtricks.com/114-magento-index-management-Cannot-initialize-the-indexer-process

我写了这个:

RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} (.+)\.learntipsandtricks\.com\/p\-(\d+)\-(d+) [NC]
RewriteRule ^(.*)$ http://learntipsandtricks.com/blog/%2/%1/%3/$1 [P]

RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} (.+)\.learntipsandtricks\.com\/(\d+)\-(.*) [NC]
RewriteRule ^(.*)$ http://learntipsandtricks.com/blog/%1/%2/%3/$1 [P]

RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} (.+)\.learntipsandtricks\.com [NC]
RewriteRule ^(.*)$ http://learntipsandtricks.com/blog/c/%1/$1 [P]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php/$1 [L]

上述代码不起作用。是否可以在htaccess中使用if else来检查所有类型的URL并相应地重写?

修改

RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} (.+)\.learntipsandtricks\.com [NC]
RewriteCond %{REQUEST_URI} ^/p-(\d+)-(\d+)$ [NC]
RewriteRule ^(.*)$ http://learntipsandtricks.com/blog/%1/[first part of sub-domain]/%2/$1 [P]

如何在这里获得子域的第一部分。

谢谢。

1 个答案:

答案 0 :(得分:1)

HTTP_HOST不包含网址路径。如果您想与网址路径匹配,请改用REQUEST_URI

RewriteCond %{REQUEST_URI} ^/p-(\d+)-(\d+)$

例如。

来自RewriteCond Directive

  
      
  • RewriteCond反向引用:这些是%N形式的反向引用(0 <= N <= 9)。 %1到%9提供对模式的分组部分(同样,在括号中)的访问,在当前条件集中,从最后匹配的RewriteCond 。 %0提供对该模式匹配的整个字符串的访问。
  •   

因此,如果您要同时捕获域和网址路径组件,则必须将HTTP_HOSTREQUEST_URI合并为一个RewriteCond

RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} (.+)\.learntipsandtricks\.com [NC]
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^(.+)\.learntipsandtricks\.com/p-(\d+)-(\d+)$ [NC]
RewriteRule ^(.*)$ http://learntipsandtricks.com/blog/%2/%1/%3/$1 [P]