Pergunta

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.

Foi útil?

Solução

HTTP_HOST doesn't include the URL-path. If you want to match against the URL-path, use REQUEST_URI instead

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

for example.

From RewriteCond Directive

  • RewriteCond backreferences: These are backreferences of the form %N (0 <= N <= 9). %1 to %9 provide access to the grouped parts (again, in parentheses) of the pattern, from the last matched RewriteCond in the current set of conditions. %0 provides access to the whole string matched by that pattern.

Therefore, if you want to capture both the domain and URL-path components, you must combine HTTP_HOST and REQUEST_URI in one 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]
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top