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).
Related
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).
So I was wondering how can I make it where I have a directory for say domain.com/main/ and in that /main I have license.php with .htaccess how am I able to make it where I can do domain.com/main/view/license/(license) and it shows them domain.com/license.php?id={license} ive tried this and it doesn't work
This is my current .htaccess that I use that im trying to make it work with
<IfModule mod_rewrite.c>
RewriteRule ^/view/license/([a-zA-Z0-9\-/\.]+)/?$ license.php?id=$1 [L]
</IfModule>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]```
Implement this rule in either a dynamic configuration file (".htaccess" style file) in the http server's DOCUMENT_ROOT folder, or, preferably, in the real host configuration:
RewriteEngine on
RewriteRule ^/?main/view/license/?$ /license.php [END]
RewriteRule ^/?main/view/license/([^/]+)/?$ /license.php?id=$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 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).
Use
RewriteRule ^main/view/license/([a-zA-Z0-9\-/\.]+)/?$ license.php?id=$1 [L]
Adding main to the rule, and removing the first slash.
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]
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]
I've been trying fix my error, first here is my .htaccess code:
# Clean Url for User Profiles
AddDefaultCharset UTF-8
Header unset ETag
FileETag None
Options +FollowSymLinks -MultiViews
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+friend\.php\?user_name=([^\s&]+) [NC]
RewriteRule ^ friend/%1? [R=301,L]
RewriteRule ^friend/([^/]+)/?$ friend.php?user_name=$1 [L,QSA]
# End Clean Url Htaccess
My intentions with this code is to make this URL http://localhost:8888/circlepanda/friend?user_name=kendrick
Display like this http://localhost:8888/circlepanda/friend/kendrick and work.
The page opens good, burr the parameter isn't parsed. How do I fix this?
There are a number of issues with your code. I tried to fix what I see, here is my version of what you probably are trying to implement:
Options -MultiViews
RewriteEngine on
# external: /..../friend?username=joe >> /..../friend/joe
RewriteCond %{QUERY_STRING} user_name=([^&]+)
RewriteRule ^friend/?$ friend/%1 [R=301,QSA]
# internal: /friend/joe >> friend.php?user_name=joe
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^friend/([^/]+)/?$ friend.php?user_name=$1 [END,QSA]
Depending on your specific situation it might be that you need to add a RewriteBase. See the official documentation for that.
If you experience an internal server error using above lines chances are that you operate a very old version of the apache http server. In that case you need to replace the [END] flag with the [L] flag.
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 provided 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).