2013-03-04 172 views
0

我想重寫我的網址到更多seo網址使用通配符子域名,我無法寫入htaccess條件並希望得到一些幫助。.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來檢查所有類型的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

HTTP_HOST不包含URL路徑。如果你要匹配的URL路徑,使用REQUEST_URI代替

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

例如。

RewriteCond Directive

  • RewriteCond反向引用:這些是的形式%N(0 < = N < = 9)反向引用。%1到%9提供對當前條件集合中來自最後匹配的RewriteCond的模式的分組部分(同樣在括號中)的訪問權限。 %0提供對該模式匹配的整個字符串的訪問。

因此,如果你想捕捉兩個域和URL路徑組件,您必須在一個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] 
+0

謝謝結合HTTP_HOSTREQUEST_URI。你知道如何獲得子域的第一部分,請參閱上面的編輯。 – 2013-03-04 21:21:11

+1

@DamodarBashyal請參閱更新的答案。 – 2013-03-04 21:34:56

+0

太棒了!謝謝。 – 2013-03-04 21:49:37