htaccess rule conflict? - apache

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]

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.

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..

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]

.htaccess Redirection for Querystrings

I have an old website that URLs were like /page.aspx?sch=XXXX&prg=YYYY. I want to redirect those old URLs to my new website's URLs. I'm trying to use an .htaccess file to do the trick but I couldn't get it work. What I want is:
/page.aspx?sch=XXXX&prg=YYYY ==> /page/sch/XXXX/prg/YYYY
If someone could helped me, I'd be very pleased.
Thanks...
If there are only these two URL parameters and they are only used in this order, you can do the following:
RewriteCond %{QUERY_STRING} ^sch=([^&]+)&prg=([^&]+)$
RewriteRule ^page\.aspx$ /page/sch/%1/prg/%2? [L,R=301]
Otherwise you will need to extract these parameters one by one and put them in in right order before doing that redirect.

The correct way to make a rewrite rule?

I have a .htaccess in my site www folder that has this rewrite rule:
RewriteRule ^(\w+)/?$ /$1.php
It works, if you type in
http://sampardee.com/urltest -
It finds urltest.php and brings it up.
However, if you type in
http://sampardee.com/urltest/
it still brings urltest.php up but the CSS stops working. I have the CSS file specified in a link tag. The same results appear also when
http://sampardee.com/urltest.php/
is accessed.
Is there any way I can fix this so that someone could type in
http://sampardee.com/urltest/
and have urltest.php come up, but yet still display the linked CSS file?
Please help :)
-Sam
The problem isn't with mod_rewrite, but with the css link (the browser tries to fetch http://[...]/urltest/css/default.css instead of /css/default.css).
Try adding a beginning slash, and changing the to:
/css/default.css
A better idea would probably be to redirect http://sampardee.com/urltest/ to http://sampardee.com/urltest.
RewriteRule ^(\w+)/$ /$1 [R]
RewriteRule ^(\w+)$ /$1.php