Rewriting URLs from old website - apache

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

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.

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.

.htaccess mod_rewrite 301 redirect with nested exceptions?

I need a little help with my .htaccess before I deploy it!
I want to 301 redirect almost everything from elementalthreads.com to ethreads.com, excluding blog/wp-content/uploads, and /pommo.
Am I doing this right?:
RewriteEngine on
#exclude old uploads folder and /pommo
RewriteCond %{REQUEST_URI} !^/(blog/wp-content/uploads|pommo) [NC]
RewriteRule (.*) http://ethreads.com/$1 [R=301,L]
Will that transfer canonical pagerank?
Here's where I know I need help:
The old site has a wordpress blog, which I've cloned on the new domain. I'd love to preserve the permalinks, which are almost 1:1, eg:
http://www.elementalthreads.com/blog/ethreads-now-on-amazon-com/ redirects to
http://ethreads.com/ethreads-now-on-amazon-com/ (note /blog/ is missing here)
And the blog index http://www.elementalthreads.com/blog/ should redirect to http://ethreads.com/blog/, which seems like an exception to the above rule, since "/blog/" should only be preserved here?
I'm stumped about how to regEx or otherwise define these last two conditions/rules. Any help would be most appreciated!
That looks correct to me. However, you should not put this live without checking it, there really is nothing preventing you from being able to test it. One thing to bare in mind is that browsers can cache 301 response codes so when testing you should use [R,L] as your flags. Once you are happy add the [R=301,L] back in before deployment.
OK for points (1) & (2)
# only redirect the blog direcotry
RewriteRule ^blog/?$ http://ethreads.com/blog/ [NC,R=301,L]
# redirect all sub folders of blog to the new domain
RewriteRule ^blog/([\w-])/?$ http://ethreads.com/$1/ [NC,R=301,L]

Using Apache mod_rewrite for conditional redirection if URL exists

I'm migrating a large site (with significant changes to each page), and want a test group of users to be automatically redirected from the current site to the new site as each page is completed.
What was
www.mysite.com/admin/somefile.php
will become
admin.mysite.com/somefile.php
As each page is migrated to the new site, I want users from a specific IP address to be automatically redirected to it. This looks like a case for the '-U' (existing URL) flag of RewriteCond. The mod_rewrite statements (for www.mysite.com) would be something along the lines of
RewriteEngine On
RewriteCond %{REMOTE_ADDR} 123\.123\.123\.123 # for test users only
RewriteCond http://admin.mysite.com/$1 -U # if new page exists
RewriteRule /admin/(.*) http://admin.mysite.com/$1 [R=302,L] # then redirect
However, this doesn't seem to work - perhaps because the $1 parameter is being referenced before being defined?
Can anyone advise how this can be achieved?
Thanks, Chris
Thanks, LazyOne – you gave me confidence to pursue it (if you make it an answer I'll accept it).
I think the problem was simply in using /admin instead of ^admin; my final mod_rewrite statements are:
RewriteEngine On
RewriteCond %{REMOTE_ADDR} 123\.123\.123\.123 # for test users only
RewriteCond http://admin.mysite.com/$1 -U # if new page exists
RewriteRule ^admin/(.*) http://admin.mysite.com/$1 [R=302,L] # then redirect
...which seems to correctly serve the page from the new site if it exists, otherwise serves the page from the old site.
You could use it this way to manipulate URI
RewriteCond %{REQUEST_URI} ^/admin/(.*)$
RewriteRule .* http://admin.mysite.com/#1 [R=302,L]
Hope this works. BTW, you can test your rewrite online here http://martinmelin.se/rewrite-rule-tester/

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]