.htaccess rewrite to redirect two different folder - php

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

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).

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]

Related

htaccess Multiple Rewrite issues

I'm new to htaccess so this may be easy to many here, but it's eluding me. I have two folders, one is "public" and the other is "admin". The htaccess code was pieced together from elsewhere but it's not properly working for me.
If www.domain.com/admin, then send it to admin/index.php
If www.domain.com/[anything else], send to public/index.php?xxx
If www.domain.com then send to pubic/index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^admin /admin/index.php [QSA,L]
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
This is the htaccess file in the public folder. I do not have an htaccess file in the admin folder.
<IfModule mod_rewrite.c>
Options -Multiviews
RewriteEngine On
RewriteBase /public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
</IfModule>
Problems:
www.domain.com/admin generates a 500 Internal Server Error
www.domain.com/anything also generate a 500 error
www.domain.com works as expected
Thanks a bunch for any help!
Use only a single set of rules, do not spread them over several places, that only makes things more complex and error prone. If possible use the real http server configuration instead of dynamic configuration files, more on that below.
RewriteEngine On
RewriteRule ^/?admin/ /admin/index.php [END]
RewriteCond %{REQUEST_URI} ^/public/index\.php$
RewriteRule ^ /public/%{QUERY_STRING} [R=301]
RewriteCond %{REQUEST_URI} ^/public/(.*)$
RewriteRule ^ /public/%1 [R=301]
RewriteRule ^/?(.*)$ public/index.php?$1 [END]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Problem with mod rewrite Rules in htaccess

I'm seeting up a new website and want to rewrite some files with parameter in the urls.
The site isn't live right now and unfortunaly i haven't lots of experience in mod_rewrite.
So whats my Problem:
I have two files: category.php and single.php
on my index i have a menu that refers to all categories via url paramater.
For instance on sitename.com/index.php you find links to:
sitename.com/category.php?c=First
sitename.com/category.php?c=Second
sitename.com/category.php?c=Third
and so on
On sitename.com/category.php?c=First for instance you find a list of all posts that refer to category first and linked to:
sitename.com/single.php?c=Frist&name=name1
sitename.com/single.php?c=Frist&name=name2
sitename.com/single.php?c=Second&name=name3
sitename.com/single.php?c=Third&name=name4
and so on
Now i try to rewirte the urls to the following structure:
sitename.com/category.php?c=First => sitename.com/First
sitename.com/category.php?c=Second=> sitename.com/Second
sitename.com/single.php?c=Frist&name=name2 =>sitename.com/First/name2
sitename.com/single.php?c=Second&name=name3 =>sitename.com/Second/name3
I used the following Code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ categroy.php?c=$1 [L,QSA]
RewriteRule ^(.*)/(.*)$ single.php?c=$1&name=$2 [L,QSA]
</IfModule>
Each RewriteRule works for its own but together i will not get the expected results.
so i tried it serval days now and i don't get it.
So hope someone here can help
Thanks a lot
There are several issues here with your attempt, but the main point is that you need to take care about the order of your rules. Since rules are applied from top to bottom you need to place more specialized rules first in the file (so further up), more general rules later ...
Here is a modified version to point you into the right direction:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [END]
RewriteRule ^/?(\w+)/(\w+)/?$ single.php?c=$1&name=$2 [END,QSA]
RewriteRule ^/?(\w+)/?$ category.php?c=$1 [END,QSA]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
These rules will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Htaccess redirecting wrong page

here is my htaccess code
# Enable rewrite engine
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)\.php$ view-product.php?catSeoUrl=$1&subCatSeoUrl=$1&productSeoUrl=$3 [QSA,L]
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)\.php$ products.php?catSeoUrl=$1&subCatSeoUrl=$1 [QSA,L]
RewriteRule ^([a-zA-Z0-9-]+)\.php$ products.php?catSeoUrl=$1 [QSA,L]
the second & third rules are working fine. Please have a look here
http://domain.tdl/consumer-electronics/mobiles.php
http://domain.tdl/consumer-electronics.php
but the First Rule for view Product also redirecting to products.php, have a look.
http://domain.tdl/consumer-electronics/mobiles/product-6.php
its strange, can you please help me to resolve this?
As soon as the third rule is applied then automatically the third rule gets applied too, since its pattern matches the target of the first rule.
With the apache http server from version 2.4 onwards you can use the END flag instead of the L flag to prevent that:
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)\.php$ view-product.php?catSeoUrl=$1&subCatSeoUrl=$1&productSeoUrl=$3 [END]
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)\.php$ products.php?catSeoUrl=$1&subCatSeoUrl=$1 [END]
RewriteRule ^([a-zA-Z0-9-]+)\.php$ products.php?catSeoUrl=$1 [END]
For older apache versions you need to add rewrite conditions:
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)\.php$ view-product.php?catSeoUrl=$1&subCatSeoUrl=$1&productSeoUrl=$3 [END]
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)\.php$ products.php?catSeoUrl=$1&subCatSeoUrl=$1 [END]
RewriteCond %{REQUEST_URI} ^([^\]+)\.php$
RewriteRule ^([a-zA-Z0-9-]+)\.php$ products.php?catSeoUrl=$1 [END]
And a general hint: 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).

.htaccess mod rewrite for product id

Evening guys,
I have been attempting to get mod rewrite working for product id's and name's but I am having no luck at all. I have tried searching here on SO but there are no clear explanations.
This is for my ecommerce demo located http://www.peakwebdesigns.co.uk/ecommerce/store
Example URL: http://www.peakwebdesigns.co.uk/ecommerce/product_page?id=3name=Bed
I want: http://www.peakwebdesigns.co.uk/ecommerce/product_page?id=3name=Bed
to be http://www.peakwebdesigns.co.uk/ecommerce/3-Bed
My URL is structured as follows:
<img src="images/'. $product_array[$key]["image"] . '"></div>' ?>
This is what I currently have in my .htaccess (I know the first few code entries work because I have removed .php and .html from files):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^([0-9])-([A-Z]+)$ product_page?id=$1name=$2 [NC,L]
Question: Due to me having /ecommerce/ included in my URL is the above setup correctly?
If not, can anyone help assist me in writing this correctly?
Thanks.
Stan.
There are many possible setups, I expect this to be the one most likely making sense for you (you offered very little information about your situation):
Best is to place such rewriting rules inside the host configuration of your http server:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/ecommerce/(\d+)-(\w+)/?$ /ecommerce/product_page?id=$1name=$2 [END]
Alternatively you can also use a dynamic configuration file. I then would recommend to place that file inside the ecommerce folder and start with something like that:
RewriteEngine on
RewriteBase /ecommerce
RewriteRule ^/?(\d+)-(\w+)/?$ product_page?id=$1name=$2 [END]
For this to work you need to enable the interpretation of dynamic configuration files fist, take a look the the AllowOverride directive in the documentation for that.
Note: in case you receive back an http status 500 (internal server error) with above rules, then chances are that you operate a very old version of the apache http server. In that case try to replace the [END] flag with the [L] flag.
Also, for the sake of precision I want to point out that you need to format the links you send out correctly. You cannot keep the code creating the links unchanged and expect the rewriting module to magically alter the literal links you send out.
And a general hint: 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).
I used the following rules to adjust URLs according to the parameters:
RewriteRule ^ecommerce/product/(.*)$ ./ecommerce/product?id=$1 [L,NC]
RewriteRule ^ecommerce/category/(.*)$ ./ecommerce/category_page?category_name=$1 [L,NC]

My htacess file is not working despite my configuration

I am tired of seeing my urls look dirty eg. http://localhost/test/postsingle?tk=Ben Affleck and Jennifer Garner File For Divorce Amid Reconciliation Rumors
so I am currently trying to make it look like this eg
http://localhost/test/postsingle/Ben Affleck and Jennifer Garner File For Divorce Amid Reconciliation Rumors
But its not working this my htacess config below
Please I will be really grateful for an answer
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^politicssingle/(\d+)*$ ./politicssingle?tk=$1
RewriteRule ^postsingle/(\d+)*$ ./postsingle?tk=$1
RewriteRule ^search/(.*)$ ./search?query=$1
Though I've seen related questions here but they're not helping
There is no rule in that file that even tries to rewrite the requests you ask about. So why should they get rewritten?
I'd say that you probably need to replace the first rewrite rule like that:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^/?test/postsingle/(.+)/? /test/postsingle?tk=$1 [END]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Categories