2 rewrite rules not working together - apache

I have a rewrite rule for my product detail page so that instead of the url being
/product-detail.php?id=3765
it is instead:
/blue-jeans
This is the rewrite rule:
RewriteRule ^([0-9a-zA-Z-]+)$ product-detail.php?slug=$1 [NC,L]
However, I need another one for my regional dealers. As soon as I add this to my .htaccess, the product detail rule stops working in that a blank page is displayed when navigating to a product. How can I get them both to work?
RewriteRule ^([0-9a-zA-Z-]+)$ dealers.php?slug=$1 [NC,L]

it's because the matching regex are identical, and the first one of them matches and so you're done. You need a wrapping php script that can sort this out for you or have some other token in your regex to limit the scope. For example you could say that all products are under /products and all dealers are under /dealers, and then do something like:
RewriteRule ^/dealers/([0-9a-zA-Z-]+)$ /dealers.php?slug=$1 [NC,L]
RewriteRule ^/products/([0-9a-zA-Z-]+)$ /products.php?slug=$1 [NC,L]

Related

htaccess page to page redirect and seo friendly urls

i have a problem with a htaccess files and i cannot figure it what is the problem.
The site has url rewriting for seo purposes in place so:
www.website.com/page/seo-friendly-url
is rewritten to
www.website.com/page.php?seo=seo-friendly-url
this is done with the following
RewriteEngine on
RewriteBase /
Rewriterule ^page/([a-zA-Z0-9_-]+)$ page.php?seo=$1 [NC,L]
Now the problem is that i have to redirect some pages that are already indexed by the search engines to their new destination as they are no more available, for example:
www.website.com/page/seo-friendly-url
has to be redirected to
www.website.com/page/another-seo-friendly-url
I have tried something like this but it is not working
Rewriterule ^page/seo-friendly-url$ page/another-seo-friendly-url [R,NC,L]
also this one is not working
Rewriterule ^page/seo-friendly-url$ page.php?seo=another-seo-friendly-url [R,NC,L]
This seems pretty stupid but i can't find the problem :-/
Thank you for your help
Ema
Edit, for anubhava:
Hi,
no i have already set the rewriting for that.
What i'm trying to achieve is redirect an already rewrited link.
Let me explain myself better:
At the moment i have this url that is indexed by Google (or any other search engine) in the form of a beautified url (seo friendly). The url has this form:
www.website.com/page/seo-friendly-url
I have already set a rule in the htaccess so the previous link is rewritten and goes to a php page with a query string that is used to display some content.
The page and the query are in this form:
www.website.com/page.php?seo=seo-friendly-url
So basically i'm using the last part of the first url as a query parameter for the second url.
This is achieved (and works) through the following code here below:
RewriteEngine on
RewriteBase /
Rewriterule ^page/([a-zA-Z0-9_-]+)$ page.php?seo=$1 [NC,L]
So far so good.
Now what i need to achieve is to redirect this url, that has been deleted:
www.website.com/page/seo-friendly-url
to go to a new page
www.website.com/page/another-seo-friendly-url
Of course the same rules applies to this new url (www.website.com/page/another-seo-friendly-url -->is already rewrited to--> www.website.com/page.php?seo=another-seo-friendly-url)
What do i need to do to do the reewriting right?
Thanks
You need this extra rule before your existing rule:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+page\.php\?seo=([^\s&]+) [NC]
RewriteRule ^ /page/%1? [R=301,L]
Rewriterule ^page/([\w-]+)$ page.php?seo=$1 [NC,L,QSA]
Just add redirects like this:
RewriteRule page/seo-friendly-url /page/new-url [R=301,L]
Important: this rules have to be above your existing rewrites because of the L flag in your rewrites
The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed.
http://httpd.apache.org/docs/current/rewrite/flags.html#flag_l
Edit
You want to redirect the old URL to avoid duplicate content (rewrite=internal, redirect=HTTP 301)
Maybe you are open for solutions thinking in another direction.
I would try to handle this in the application, no through rewrites. Right now the GET parameter seo is handled in page.php. Isn't it an idea to extend this in that way one product can be identified through multiple seo aliases? If one product has to be taken off a similar one will then own this alias (simply a change of one row in the database).
As I don't know what software you are using this may be not possible.

RewriteRule that works with paginated pages

I am trying to define a RewriteRule
Within the site I have pages like this:
index.php?pag=company&id=853
And I've defined this rule:
RewriteRule ^/([a-zA-Z0-9])/([0-9]+)$index.php?pag=company&id=$2 [NC, L]
Trying to get this result:
company/university-of-gloucestershire/1656
This rule works, however doesn't work for paginated pages, like:
index.php?pag=company&id=853&page=2
I tried:
RewriteRule ^/(.*)/([0-9]+)/([0-9]+)$ index.php?pag=company&id=$2&page=$4 [NC, L]
Trying to get to:
company/university-of-gloucestershire/1656/page/2
Can anyone point out what's wrong?
Looks like a typo. $4 doesn't exist in your example rule for pagination. Also if this is in .htaccess, the initial slash shouldn't be there. .* is greedy, so I've replaced it with any character, not a /.
RewriteRule ^([^/]+)/([^/]+)/([0-9]+)/([^/]+)/([0-9]+)$ index.php?pag=$1&id=$3&page=$5 [NC,L]
company/university-of-gloucestershire/1656/page/2 would become:
index.php?pag=company&id=1656&page=2
That rule needs to precede this one because you still have to catch the pages where someone's not specifying a page number:
RewriteRule ^([^/]+)/([^/]+)/([0-9]+)(/page/?)?$ index.php?pag=$1&id=$3&page=1 [NC,L]
company/university-of-gloucestershire/1656/ and
company/university-of-gloucestershire/1656/page would become:
index.php?pag=company&id=1656&page=1

How to add a correct rule for page rewriting in .htaccess?

I'm trying to do a paginate system on my website.
Before, I had it :
RewriteRule ^famille/([^/]*)-([^-]*)\.html$ /?fond=famille&id_rubrique=$1 [L]
And I could get to my page like this :
http://blabla.net/famille/131-articles_de_cave.html
So, now I add a rule for the paginate :
RewriteRule ^famille/([^/]*)-([^-]*)-([^-]*)\.html$ /?fond=famille&id_rubrique=$1&page=$3 [L]
If a go on :
http://blabla.net/famille/131-articles_de_cave-2.html (for the page two) it works.
But if I am on the fist page, I get 404 error :
http://blabla.net/famille/131-articles_de_cave.html
How fix this problem ? (I need to access on the first page without write the page number)
Are you using both rules or just one?
If you are using both rules in this order:
RewriteRule ^famille/([^/]*)-([^-]*)-([^-]*)\.html$ /fond=famille&id_rubrique=$1&page=$3 [L]
RewriteRule ^famille/([^/]*)-([^-]*)\.html$ /fond=famille&id_rubrique=$1 [L]
then the first group is not exclusive enough. The greedy match will match the first -. Try this:
RewriteRule ^famille/([^-/]*)-([^-]*)-([^-]*)\.html$ /fond=famille&id_rubrique=$1&page=$3 [L]
RewriteRule ^famille/([^-/]*)-([^-]*)\.html$ /fond=famille&id_rubrique=$1 [L]
Or maybe just swap the rules?
PS: As per http://www.w3.org/Provider/Style/URI you might want to leave out the .html...
The hyphen before the page number is your problem. Make it optional, try this:
RewriteRule ^famille/([^/]*)-([^-]*)-?([^-]*)\.html$ /?fond=famille&id_rubrique=$1&page=$3 [L]

2 url rewrite rule for 1 dynamic page

I have a dynamic page called show.php. The page is dynamic and the url may be either show.php?name=john-doe or show.php?category=student.
I'm trying to create a rewrite rule that turns the url into /show/john-doe.html for names or /show/student.html for category.
This is what I have in my .htaccess so far.
RewriteRule ^show/([^/]*)\.html$ show.php?name=$1 [L]
RewriteRule ^show/([^/]*)\.html$ show.php?category=$1 [L]
Currently, only the name rule works but the category rule doesn't. What's wrong?
The problem is that you are sending all show/xxx.html to the same URL (the first one). Since both rewrite rules are using exactly the same parameter only the first one will work.
You could solve this in two different ways.
Either you use show.php?id=xxx and accept both name and category in your PHP and detirmine there what page to show.
Or you use two different types of urls in your rewrite to get show/category/student.html and show/student/john-doe.html like so:
RewriteRule ^show/student/([^/]*)\.html$ show.php?name=$1 [L]
RewriteRule ^show/category/([^/]*)\.html$ show.php?category=$1 [L]

Redirecting to same page with .htaccess

From my .htaccess file:
RewriteRule ^showPAGE.php page [NC,R=301]
RewriteRule ^page showPAGE.php [NC,L]
I want users going to url domain.com/showPAGE.php to be redirected to domain.com/page .
When domain.com/page is being entered, I want it to show the content of the file showPAGE.php.
Is that possible to do?
The above results an infinite redirection loop.
Thanks
You're trying to do something that's very tricky. The problem is that, by design, the RedirectRule directive always triggers again the complete set of rules. You can only get out of the loop when you obtain a final URL that does not match any of the rules and that's the tricky part since you are reusing the showPAGE.php name.
My best attempt so far involves adding a fake hidden string:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/showPAGE\.php
RewriteCond %{QUERY_STRING} !^internal
RewriteRule ^ http://%{HTTP_HOST}/page [NC,R=301,L]
RewriteRule ^page$ showPAGE.php?internal [NC,L]
It works but it's not pleasant. Definitively, it's easier to handle the redirection from with PHP or to simply pick another name.
The redirect from showPAGE.php to page needs to have [L] so that it will stop processing and redirect at once, rather than going on and applying other rules (which at once map it back to showPAGE.php). Try this:
RewriteRule ^showPAGE.php page [NC,R=301,L]
RewriteRule ^page showPAGE.php [NC,L]