.htaccess SEO Friendly URLs not working with two parameters - php

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]

Related

Different htaccess directory and subdirectory redirect

I am trying to have clean urls on my site and here is the mapping I'm trying to achieve:
mysite.tld/directory/ is going to > mysite.tld/page.php?q=directory
mysite.tld/tags/directory/ is going to > mysite.tld/tag.php?q=directory
I have made an htaccess file at the root of my site wiht the folling code:
RewriteEngine On
RewriteRule ^([^\/]+)\/?$ pages.php?q=$1 [L,QSA]
RewriteRule ^tags/([^/d]+)/?$ tags.php?q=$1 [L,QSA]
For some reason, the tag redirect works but is then "eaten" by the other rule of redirection. Not sure what to do to prevent second rule from doing this.
Answer is as follow:
RewriteEngine On
RewriteRule ^tags/([^/d]+)\/? /tags.php?q=$1 [L,QSA]
RewriteCond %{REQUEST_URI} !/tags
RewriteRule (.*) pages.php?q=$1 [L,QSA]

.htaccess rewriteRule conflict

I am writing a code and build an htaccess file which has the following contents:
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?id=$1 [NC,L] # Handle page requests
RewriteRule ^category/([A-Za-z0-9-]+)/?$ category.php?cat=$1 [NC,L] # Handle category requests
RewriteRule ^author/([A-Za-z0-9-]+)/?$ author.php?auth=$1 [NC,L] # Handle author requests
The first rule works fine but the latter seems having conflict with the first rule. Everytime I access a url at http://example.com/category/foobar or http://example.com/author/fooauthor/ I always get a message that the page cant be found because it is still trying to open the index file.
Is there any possibility to get around with this?
You can set general rule in the last line:
RewriteEngine on
RewriteRule ^category/([A-Za-z0-9-]+)/?$ category.php?cat=$1 [NC,L]
RewriteRule ^author/([A-Za-z0-9-]+)/?$ author.php?auth=$1 [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?id=$1 [NC,L]
This issue happened because first line support all urls

htaccess rewrite rules conflict

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.

Query string url to seo friendly url

i am trying to rewrite a URL for SEO purpose.
The old URL is:
http://www.example.com/recipe_local.php?hl_cusine=1
The new URL should be like bellow and automatic redirect to this url if user come above url
http://www.example.com/recipes/healthy-recipes
My Code in the .htaccess is:
RewriteEngine on
RewriteRule ^recipes/healthy-recipes/$ recipe_local.php?hl_cusine=$1 [NC,L]
RewriteRule ^recipes/healthy-recipes$ recipe_local.php?hl_cusine=$1 [NC,L]
Even after hours of research, i have no clue why this is not working :(
The $1 in your rewrite is pointing to a backreference you never capture.
Unless you have a RewriteCond you are not showing?
try:
RewriteEngine on
RewriteRule ^recipes/healthy-recipes/$ recipe_local.php?hl_cusine=1 [NC,L]
RewriteRule ^recipes/healthy-recipes$ recipe_local.php?hl_cusine=1 [NC,L]

rewrite url htaccess cakephp paginator

RewriteRule result/([^\.]+) /job/search/result?k=$1 [L]
i have this code working well with urls like this
www.example.com/job/search/result/keyword
will give me the result of "keyword"
but the problem is that cakephp paginator is generating a link
like this
result/page:2?k=keyword
how can i write a rule for that
to fix the problem, i tried this but without a luck
RewriteRule result\/page:([0-9]+)\?k=([^\.]+) /job/search/result/page:$1?k=$2 [L]
so my new rules are
RewriteRule result\/page:([0-9]+)\?k=([^\.]+) /job/search/result/page:$1?k=$2 [L]
RewriteRule result/([^\.]+) /job/search/result?k=$1 [L]
I've fixed it by adding a rewriting condition
RewriteCond %{REQUEST_URI} !^/job/search/result/page:
so that the rewrite rule will only respond to
/job/search/result/keyword

Categories