htaccess rewrite rules conflict - php

www.example.com/category/sub_category/Having problem with the rewririte rules in .htaccess file.
my current .htaccess file looks like this.
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9]+)$ products.php?cat=$1&id=$2 [NC,L]
RewriteRule ^([^/]*)/([0-9a-zA-Z_-]+)/([0-9]+)$ product_categories.php?cat=$2&id=$3 [NC,L]
RewriteRule ^(.*)/(.*)/([0-9a-zA-Z_-]+)/([0-9]+)$ product_details.php?cat=$3&id=$4 [NC,L]
RewriteRule ^([0-9]+)$ gallery.php?id=$1 [NC,L]
I'm trying create urls like the following.
www.example.com/product_name/1
www.example.com/category/sub_category/21
www.example.com/category/sub_category/product_name/233
www.example.com/gallery/872
www.example.com/gallery/872 is redirecting to www.example.com/category/sub_category/872 instead of gallery.php?id=872
edit:corrected url from www.example.com/gallery/872 to www.example.com/category/sub_category/872.

Your issue is that the first rule matches, the last one can never get applied...
RewriteEngine on
RewriteRule ^gallery/([0-9]+)/?$ gallery.php?id=$1 [NC,L]
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9]+)$ products.php?cat=$1&id=$2 [NC,L]
RewriteRule ^([^/]*)/([0-9a-zA-Z_-]+)/([0-9]+)$ product_categories.php?cat=$2&id=$3 [NC,L]
RewriteRule ^(.*)/(.*)/([0-9a-zA-Z_-]+)/([0-9]+)$ product_details.php?cat=$3&id=$4 [NC,L]
Rule of thumb: first the specific exceptions, then the more general rules.
The NC flag does not make sense if you also specify both, lower and upper case letters in your regex patterns. It is either/or, not and.
(note: I also included the correction #anubhava posted in his answer)

Your last rule will need regex modification since you're matching /gallery/872 and your pattern is only matching 1 or more digits.
RewriteRule ^gallery/([0-9]+)/?$ gallery.php?id=$1 [NC,L,QSA]
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9]+)$ products.php?cat=$1&id=$2 [QSA,L]
RewriteRule ^([^/]*)/([0-9a-zA-Z_-]+)/([0-9]+)$ product_categories.php?cat=$2&id=$3 [QSA,L]
RewriteRule ^(.*)/(.*)/([0-9a-zA-Z_-]+)/([0-9]+)$ product_details.php?cat=$3&id=$4 [QSA,L]
Also you need to recorder your rules like I showed above.

Related

.htaccess SEO Friendly URLs not working with two parameters

I am trying to create SEO friendly URLs for my site and I have come to problem that I can´t solve. I can´t figure out how to setup the rewrite rule so it will show for example like this www.mysite.com/shows/late-night and www.mysite.com/news/title-of-article. Here is my htaccess file:
RewriteEngine on
RewriteRule ^(.*)/([a-z_-]+) index.php?switch=$1&shows=$2 [NC,L]
RewriteRule ^(.*)/([a-z_-]+) index.php?switch=$1&article=$2 [NC,L]
RewriteRule ^shows/$ index.php?switch=shows [NC,L]
RewriteRule ^articles/$ index.php?switch=article [NC,L]
The rewrite rule works if I comment out the first and the third line, but how I do so both will work.
1st and 2nd rule cannot coexist because they are matching same URI pattern. Only first will all the time. Tweak your rules like this:
RewriteEngine on
RewriteRule ^(shows)/([a-z_-]+)/?$ index.php?switch=$1&shows=$2 [NC,L,QSA]
RewriteRule ^(news)/([a-z_-]+)/?$ index.php?switch=$1&article=$2 [NC,L,QSA]
RewriteRule ^(shows|news|articles)/?$ index.php?switch=$1 [NC,L,QSA]

.htaccess rewrite $_GET variables

I have the following page:
http://localhost/?news=19&page=2
I want to rewrite this so that it goes kind of as follows
http://localhost/news/19/page/2
This is my .htaccess file, but my code dont work :(
RewriteEngine ON
RewriteBase /
RewriteRule ^page/(.*)$ /?page=$1 [L]
RewriteRule ^news/(.*)$ /?news=$1 [L]
RewriteRule ^news/(.*)/page/(.*)$ /?news=$1&page=$2 [L]
You should specify more precicely what the allowed type of character is and you should make sure your previous rules don't cause your later rules to be ignored.
So you should put your most specific rule first and if you want digits for your news- and page ID's, you should use for example:
RewriteRule ^page/(.*)$ /?page=$1 [L]
RewriteRule ^news/(\d*)/page/(\d*)$ /?news=$1&page=$2 [L]
^^ just a digit instead of any character `.`
RewriteRule ^news/(.*)$ /?news=$1 [L]

Not Found when rewriting using same rule as another rewrite

So I have a rewrite doing article?id=10 which goes to article/10 and I'm trying to do another like this.
I want to go from site?=website.com to site/website.com. Alright, seems like an easy task. So I do
RewriteRule ^site/([0-9]+)$ site.php?id=$1
Which is the same rule as the article rewrite basically. Now I go to site/10 for example and I get
Not Found
The requested URL /site/sdfsd was not found on this server.
Why would this happen if it's the same thing as the other rewrite which works fine?....
Options -MultiViews
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^article/([0-9]+)$ article.php?id=$1 [L,QSA,NC]
RewriteRule ^site/([0-9]+)$ site.php?id=$1
# rewrite from /dir/file/ to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
Try
RewriteRule ^site/(.+)$ site.php?id=$1 [L]
[0-9]+ only matches numbers.
Basically your rewriting rule is bad
RewriteRule ^site/([0-9]+)$ site.php?id=$1 will only match numbers
You'll could use instead
RewriteRule ^site/(.*)$ site.php?id=$1
(wildcards)
And I'll have to say that the expression "site/whatever.com" points in reality to
site.php?id=whatever.com - reverse statement is false
You rule does not match because the role only allow numbers. In other words your rule say:
Start with "site/" following a number (0-9).
Your rule must be
RewriteRule ^site/([0-9a-zA-Z]+)$ site.php?id=$1
You seem to have two problems.
[0-9] only matches numbers, so it won't work with letters.
Also you need to add the [L] flag to your rules to make sure it stops at the first match.
RewriteRule ^site/(0-9a-zA-Z+)$ site.php?id=$1 [L]

rewrite URL using .htaccess only works on first query string

I'm trying to rewrite my url using htaccess but it only worked on the first query string
what I want is to rewrite this URL
acnologia.com/index.php?page=images&tag=nature
into something like this acnologia.com/images/nature
it does work on the first query string acnologia.com/images
but doesn't work if i add another directory after "images"
this is my .htaccess code:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1&tag=$2
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?page=$1&tag=$2
There's only one group in your original rule, so there's no $2.
Try this one:
RewriteRule ^([a-z\d_-]+)/([a-z\d_-]+)$ index.php?page=$1&tag=$2 [NC,L]
\d equals 0-9, and NC means nocase
The very basic rule will be like this
RewriteRule ^(.*)/(.*)$ index.php?page=$1&tag=$2
And more specific will be
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?page=$1&tag=$2
which #Andrew answered in comment.
Your final rewrite rule should be following. RewriteCond is specified so that js, css, png etc. files do not match this rewrite rule.
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?page=$1&tag=$2 [L,NC]
This tutorial is helpful.

htaccess problem in php

There are two kind of pages -
www.mysite.com/about.php
www.mysite.com/winners.php?y=2008
I want to make them like -
1. www.mysite.com/about
2. www.mysite.com/winners/2008
my htaccess is like -
RewriteEngine On
#removing .php ext(type-1)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
#for type-2 (with parameter)
RewriteRule winners/([0-9]+)/?$ winners.php?y=$1 [L]
RewriteRule winning-photograph/([0-9]+)/?$ winning-photograph.php?y=$1 [L]
RewriteRule award-giving/([0-9]+)/?$ award-giving.php?y=$1 [L]
RewriteRule album-launch/([0-9]+)/?$ album-launch.php?y=$1 [L]
RewriteRule district-fest/([0-9]+)/?$ district-fest.php?y=$1 [L]
RewriteRule lifetime-achievement-awards/([0-9]+)/?$ lifetime-achievement-awards.php?y=$1 [L]
RewriteRule critics-award-for-tv/([0-9]+)/?$ critics-award-for-tv.php?y=$1 [L]
RewriteRule photography-book/([0-9]+)/?$ photography-book.php?y=$1 [L]
The two section of this htaccess are not working at the same time. what can I do? also - is there a way to simplify the 2nd section?
Your first RewriteRule is basically preventing all the other rules from executing. It does that rule for both types of your URLs, then the [L] flag stops it from evaluating the other rules.
I would say put that rule at the very end. Do all the more-specific ones first, then have that as a generic one for all other cases, only to be evaluated if the rest of the rules didn't match the current URL.

Categories