0

I want to rewrite my urls to more seo urls using wildcard sub-domains and I am unable to write htaccess conditions and hoping to get some help with it.

I have this url:

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

I want to rewrite as:

http://magento.learntipsandtricks.com/

Change pagination link:

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

to:

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

Finally change article url:

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

to:

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

I wrote this:

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]

The above code doesn't work. Is it possible using if else in htaccess to check all types of urls and rewrite accordingly?

EDIT:

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]

How can I get first part of sub-domain here.

Thank you.

4

1 回答 1

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 路径组件,则必须将HTTP_HOSTandREQUEST_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]
于 2013-03-04T16:00:39.833 回答