user260523
user260523

Reputation: 145

.htaccess rewrite to redirect two different folder

If url hit to redirect to two navigation.

First condition : Some countries product information are already built

www.example.com/product-india  => product-india/index.php
www.example.com/product-us  => product-us/index.php

Second condition : remaining countries are going build dynamically

www.example.com/product-japan => product/index.php
www.example.com/product-australia => product/index.php
www.example.com/product-uk => product/index.php

and so on for other countries

What i have tried:

RewriteEngine on
RewriteRule ^/product-india /product-india/index.php [L]
RewriteRule ^/product-us /product-us/index.php [L]
RewriteRule ^/product-* /product/index.php?cat=$1 [L]

but pages are satisfy this condition only

RewriteRule ^/product-* /product/index.php?cat=$1 [L]

what doing wrong in my code. Please help me and thanks in advance

Upvotes: 0

Views: 128

Answers (2)

anubhava
anubhava

Reputation: 785146

You can have your rules as this in site root .htaccess:

RewriteEngine On

# if <uri>/index.php exists then use it
RewriteCond %{DOCUMENT_ROOT}/$1/index..php -f
RewriteRule ^([\w-]+)/?$ $1/index.php [L]

# otherwise use product/index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(product-\w+)/?$ product/index.php?cat=$1 [L,QSA"NC]

Upvotes: 0

arkascha
arkascha

Reputation: 42915

It is unclear what exactly you want to achieve, but we can make two suggestions...

By the way... ^/product-* is not the regular expression you want. We have to assume you don't really know how regular expressions are build. I suggest you start reading about them. They are a very important and mighty tool for a developer when having to deal with simple text analyzing tasks.

Here are two suggestions for working internal rewritings (you did not implement any external redirections at all yourself):

A plain internal rewriting as you asked in the question you wrote:

RewriteEngine on
RewriteRule ^/?product-india/?$ /product-india/index.php [END]
RewriteRule ^/?product-us/?$ /product-us/index.php [END]
RewriteRule ^/?product-\w*/?$ /product/index.php [END]

Another one handing the "country" over as category argument:

RewriteEngine on
RewriteRule ^/?product-india/?$ /product-india/index.php [END]
RewriteRule ^/?product-us/?$ /product-us/index.php [END]
RewriteRule ^/?product-(\w+)/?$ /product/index.php?cat=$1 [END]

In case you receive a http status 500 using those rules ("internal server error"), chances are that you operate a very old version of the apache http server. In that case replace the [END] flags with the old [L] flag, most likely that will work equally fine here, though that depends on your specific setup.

The above rule sets will work likewise in the real http servers host configurations and in dynamic configuration files.

And a general hint on that: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only supported as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

Upvotes: 1

Related Questions