htaccess page to page redirect and seo friendly urls - apache

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.

Related

Rewriting URLs from old website

I have an old website that uses links such as:
www.oldsite.com/pages.asp?pageid=123456
And have a new website that uses links such as: newsite.com/about
The old site appears in search results like that and I need to rewrite the owld URLs to the new site. I have searched for hours and hours and tried multiple things (none have exactly matched my situation) concerning RewriteRules and such and none have worked. I'm using
redirect /pages.asp /
now as a temporary solution, but I need the various links in search results to go to the new location (as in ...id=12345 goes to /about).
Any help would be greatly appreciated. Thank You!
Clarification:
The old website is completely out of my control, it is 301 redirecting to the new site. So the actual URLs I want to redirect are in the form newsite.com/pages.asp?pageid=123456.
I want to redirect the old pages to matching new pages. Some Examples:
www.newsite.com/pages.asp?pageid=123456 >> newsite.com/about
www.newsite.com/pages.asp?pageid=85544 >> newsite.com/contact
If you have only 10 pages, you can do it with simple rules instead of a map.
You can put this code in your htaccess (which has to be in root folder)
RewriteEngine On
# example rule for "about"
RewriteCond %{QUERY_STRING} ^pageid=123456$ [NC]
RewriteRule ^pages\.asp$ /about? [R=301,L]
# example rule for "contact"
RewriteCond %{QUERY_STRING} ^pageid=85544$ [NC]
RewriteRule ^pages\.asp$ /contact? [R=301,L]
# and so on for your other pages ID...

How to simulate directories with a htacess file (mod_rewrite)?

what I try to do is to simulate directories with the help of a htaccess file.
I have a website with a file like this:
http://www.domain.com/filename.php?t=yeah-a-title-2014
Now, I would like to rewrite the URL above to the following:
http://www.domain.com/directory1/yeah-a-title-2014/
If a visitor enters one of the two URLs, he should see the second one in his address bar but the content of the filename.php?t=yeah-a-title-2014 should be displayed.
I have no idea how to realize this.
Any ideas?
This is better known as SEO-urls (search engine optimized), SEF-urls (search engine friendly), fancy urls and a couple more of those terms. The basic problem with these kind of constructions, is that they cause an infinite loop if not implemented correctly, and therefore usually the THE_REQUEST trick is used, because %{THE_REQUEST} is always equal to the request, even if the url is rewritten, which in turn prevents the external redirect from matching if the internal rewrite matches.
#External redirect
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /filename\.php\?t=(.*)\ HTTP
RewriteRule ^ /directory1/%2/ [R,L]
#Change [R,L] to [R=301,L] after ALL rules do what you want them to do, and before your site goes live
#Internal rewrite
RewriteRule ^directory1/([^/]+)/?$ /filename.php?t=$1 [L]

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.

301 redirect query string to SEO friendly URLs through .htaccess

I’ve written some code on my .htaccess file which allows the use of SEO friendly URLs instead of ugly query strings. The following code rewrites the SEO friendly version in the browser to the query string version on the server.
RewriteEngine On
RewriteRule ^seo/([^/]*)/$ /directory/script.php?size=large&colour=green&pattern=$1 [L]
So that the ugly
http://www.mysite.com/directory/script.php?size=large&colour=green&pattern=striped
Is now beautiful
http://www.mysite.com/directory/seo/striped/
Just to explain the code a bit; seo is there to add more keywords to the URL, /directory/ is the directory in which the .htaccess file is located, parameters size=large and colour=green never change, while pattern=$1 can be many different values.
The above code works perfectly. However, the problem is I am now stuck with two URLs that point to exactly the same content. To solve this, I would like to 301 redirect the old, ugly querystrings to the SEO friendly URLs. What I have tried so far does not work - and Google is not being particularly friendly today.
Can anybody offer working code to put in my .htaccess file that redirects ugly to new URL, while retaining the rewrite? Thanks!
This should do the trick:
RewriteEngine On
## Redirect to pretty urls
# The '%1' in the rewrite comes from the group in the previous RewriteCond
RewriteCond %{REQUEST_URI} !seo
RewriteCond %{QUERY_STRING} ^size=large&colour=green&pattern=([a-zA-Z]*)$
RewriteRule (.*) /directory\/seo\/%1\/? [L,R=301]
## Rewrite to long url, additional parameter at the end will cause
## the internal redirect not to match the previous rule (would cause redirect loop)
RewriteRule ^directory\/seo\/([^/]*)/$ /directory/script.php? size=large&colour=green&pattern=$1&rewrite [L]
You can also match the size and colour if needed, by changing those to regex groups as well, and using the corresponding %N
Hope this helps.
Not tested, but this may work...
RewriteRule ^directory/script.php?size=large&colour=green&pattern=(.*)$ /seo/$1/? [R=301,NE,NC,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.