htaccess rewrite/redirect - apache

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

Related

URL re-direction using re-write rule not working for apache

I have a url which is like https://domain name/secure/RapidBoard.jspaa?rapidView=848&projectKey=QEFASD
and this needs to be redirected to
https://<different domainname>/secure/RapidBoard.jspa?projectKey=FIFAASD
Tried many options but no success. Pls help
I'm trying to decipher exactly what you need and Apache rewrites are generally a pain to deal with anyway without being able to do some trial and error testing on the fly.
You'll probably want to do a condition based off the query string, similar to this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^rapidView=(.*)&projectKey=(.*)
RewriteRule (.*) https://www.foo.com/secure/RapidBoard.jspa?projectKey=%2 [R=301,L]
You should be able to find similar examples all over the place:
Various Stack posts about mod_rewrite with query params
RewriteCond for url with parameters
You can also do some basic testing here:
htaccess tester
I plugged in the above rewrite rule in the tester to verify that it might actually work for you.

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.

Rewrite URL SEO

My site has a url like this
http://www.mydomain.com/index.php?page=doctors&docid=99
Using SEO rewrite
RewriteRule ^doctors/([0-9]+)/(.*).html$ index.php?page=doctors&docid=$1 [L]
I get
www.mydomain.com/doctors/13/Dr.-John-Smith.html
But I want a more friendly url like this
www.mydomain.com/Dr.-John-Smith.html
How can I achive this url.
REWRITING QUERY STRINGS
If you are hired to recreate an existing website from scratch, you might use URL rewriting to redirect the 20 most popular URLs on the old website to the locations on the new website. This could involve redirecting things like prod.php?id=20 to products/great-product/2342, which itself gets redirected to the actual product page.
Apache’s RewriteRule applies only to the path in the URL, not to parameters like id=20. To do this type of rewriting, you will need to refer to the Apache environment variable %{QUERY_STRING}. This can be accomplished like so:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=20$
RewriteRule ^prod.php$ ^products/great-product/2342$ [L,R=301]
RewriteRule ^products/(.*)/([0-9]+)$ ^productview.php?id=$1 [L]
In this example, the first RewriteRule triggers a permanent redirect from the old website’s URL to the new website’s URL. The second rule rewrites the new URL to the actual PHP page that displays the product.
I hope this helps, find out a lot more about rewrites here:
http://coding.smashingmagazine.com/2011/11/02/introduction-to-url-rewriting/
It's laid out in a pretty straight forward fashion. Hope that helps, please let me know if you have any other questions. Thank you.

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 redirects with dynamic URLs

I am having problems doing a redirect for some URLS, the ones from the old site that have a ? sign, won't redirect, every other URL will do.
example:
OLD:
/laser-alignment-resources/presentations.cfm?pres=diaphragm
NEW:
http://www.acquip.com/en/presentations/47-presentation-internal-laser-diaphragm-alignment
Won't work, I am sure I did something wrong but I am a n00b when it comes to .htaccess
all the URLs are in the same format, 14 in total:
OLD:/laser-alignment-resources/presentations.cfm?pres=gas
New:http://www.acquip.com/en/presentations/48-presentation-gas-turbine-thermal-alignment
OLD: /laser-alignment-resources/presentations.cfm?pres=train
NEW:http://www.acquip.com/en/presentations/49-presentation-complete-machine-train-alignment
any help will be appreciated.
Your question isn't clear on how you're trying to perform the redirects in your .htaccess file, so for next time I'd recommend that you post some samples of the code that you've tried so that we can help you more easily.
Based on your Apache tag and the problem you're describing, I'm going to assume that you were using mod_alias though, trying to do something like this:
Redirect 301 /laser-alignment-resources/presentations.cfm?pres=diaphragm http://www.acquip.com/en/presentations/47-presentation-internal-laser-diaphragm-alignment
This doesn't work though, as the Redirect directive seems to only examine the path portion of the request, and not the query string.
If you have mod_rewrite available, you can setup rewrite rules for the URLs that you need to redirect based on their query strings. This would look something like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} =pres=diaphragm
RewriteRule ^laser-alignment-resources/presentations\.cfm$ http://www.acquip.com/en/presentations/47-presentation-internal-laser-diaphragm-alignment [R=301,L]
RewriteCond %{QUERY_STRING} =pres=gas
RewriteRule ^laser-alignment-resources/presentations\.cfm$ http://www.acquip.com/en/presentations/48-presentation-gas-turbine-thermal-alignment [R=301,L]
...and so on. You can keep your currently working Redirect statements, as they should work fine side-by-side with these rules.