2 url rewrite rule for 1 dynamic page - apache

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]

Related

2 rewrite rules not working together

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]

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.

htaccess rewrite/redirect

I am having some problems constructing a rewrite rule. The url I want to rewrite and ultimately redirect has a search query in it and looks like this:
http://www.mysite.com/pages.php?category=fruit
I would like to redirect it to:
http://www.mysite.com/pages.php/fruit
The original address does NOT exist any more. I have tried to construct a rewrite but this is not quite working how I want it to work
RewriteEngine on
RewriteCond %{QUERY_STRING} =category=fruit`
RewriteRule ^pages\.php$ pages.php/fruit/ [L,R=301]
goes to
http://www.mysite.com/home/linux123/m/mysite.com/user/htdocs/pages.php/fruit/
Any advice on fixing the construction of the rewrite rule would be great. Thanks in advance.
The way URL rewriting works is that it takes non-existant URL and rewrites it to point to the one that exists. The non-existant URL is more of presentation thing rather than a functional thing. You are doing it the other way round, the links on your web pages should be like http://www.mysite.com/pages.php/fruit and when the user clicks on them they should internally be forwarded to something like this http://www.mysite.com/pages.php?category=fruit. The rewrite rule has to be written accordingly which would be
^pages\.php/([A-Za-z])*$ pages.php?category=$1 [NC,L]
if the category is strictly alphabetical otherwise for alphanumeric
^pages\.php/([A-Za-z0-9])*$ pages.php?category=$1 [NC,L]
You can even test your regex rewrite rules using this online validator;
Regex Validator
Hope this helps..

htaccess rule conflict?

In my .htaccess file I have the following rule defined.
RewriteRule t/([^.]+)/$ /video/tag.php?tag=$1 [QSA]
This rule is working fine for tag pages. When I am accessing URL as http://example.com/video/t/funny/ then its displaying all the result with tag funny.
But When I am accessing a URL http://example.com/video/script/common/video.php Then its displaying the TAG page with search tag results for tag common. I think there is some confliction between the URL and .htaccess rule.
Its taking /t/common/ and applying htaccess rule on this. How to solve this issue??
Your rewrite rule is matching the string in question. You want to change it to be like this:
RewriteRule ^/video/t/([^.]+)/$ /video/tag.php?tag=$1 [QSA]
Basically, what is happening is that since you didn't specify the START of your rule, it was matching the end of "script" in your url and taking it away from there.
If you want a less specific string, try this one:
RewriteRule /t/([^.]+)/$ /video/tag.php?tag=$1 [QSA]

Apache Mod-Rewrite Question

I have a PHP scripted named index.php inside a folder named blog. There are three different views.
http://www.myDomain.com/blog/index.php
http://www.myDomain.com/blog/index.php?tags=list of categories
http://www.myDomain.com/blog/index.php?post=name of post
I would like to change the view based on the URL.
/blog redirects to number 1 above
/blog/name-of-category redirects to numbe 2 above
/blog/name-of-category/name-of-post redirects to number 3 above.
Right now I have the following mod_rewrite rules.
RewriteRule ^blog$ blog/index.php [L]
RewriteRule ^blog/(.+)/(.+)$ blog/index.php?post=$2 [L]
RewriteRule ^blog/(.+)$ blog/index.php?tags=$1 [L]
This does not work, and I'm not sure why. Right now it always redirects to the last URL:
blog/index.php?tags=$1
And the GET data contains "index.php."
Also, if add a forward slash to the final rule like so:
RewriteRule ^blog/(.+)/$ blog/index.php?tags=$1 [L]
All redirects work fine. The problem is, I'm required to have a forward slash at the end of the URL if I want the category view.
Any ideas what's happening here? how I can fix this?
Thanks for the replies. I figured out that my problem was a side effect of having my scripts inside the folder named "blog". Here's what index.php looked like:
<?php
define ('BASE_PATH', "../blog/");
include_once(BASE_PATH . 'controller/Controller.php');
$controller = new Controller();
$controller->invoke();
See the problem? Because my script's base path was "blog", mod_rewrite was rewriting all my references inside the program. By renaming my script folder to blogScript, it fixed the problem.
In a regular expression, . matches any character (including a / character) so try doing ^blog/([^/]+)$ instead to match any character except a /.
You could write it as follows.
RewriteRule ^blog/?$ blog/index.php [L]
RewriteRule ^blog/(.+?)/(.+?)/?$ blog/index.php?post=$2 [L]
RewriteRule ^blog/(.+?)/?$ blog/index.php?tags=$1 [L]