Remove links from Google after adding rewrite rules in Apache - apache

I have a website that used to use php "standard" addresses, such as index.php?id=10.
Now I've configured mod_rewrite to have better URLs, therefore i added the rule
RewriteRule ^/([0-9]+)/?$ /index.php?id=$1 [QSA]
Consequently, every time someone surfs to /10 he or she will be redirect to index.php?id=10.
As the redirect is identical to the old address, Google continues to give as results the old php-style URLs, and some results are now duplicated when you perform a search in Google.
I subscribed to Google Webmaster Tools and added a sitemap for the website, leaving only the rewrite-style addresses, but after more than two months the old URLs still appear. Is there a way to remove them from Google?
Thanks a lot!

Redirect incoming old-style URLs to a site returning 404 and google will remove them. Incoming new link's wont be affected as long as you stay with interal rewrites (and exclude the resulkts from being rewritten again, either by having the rule after the others ur uisng the L flag).
This could look like this (untested, but you should at least get the idea):
RewriteRule ^/([0-9]+)/?$ /index.php?id=$1 [QSA,L]
RewriteRule ^/index.php?id=([0-9])$ - [R=404,L]
Sending the permanent redirect status code 301 (plus location header) to redirect to the new URLs should also work, in that case google won't remnove the entries but correct the URL.
RewriteRule ^/([0-9]+)/?$ /index.php?id=$1 [QSA,L]
RewriteRule ^/index.php?id=([0-9])$ /$1 [R=301,L]
You should, however, keep the old style URLs functional anyway, because Cool URIs don't change.

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.

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]

htaccess permanent redirect and remove string from url

How can I go about getting visitors who enter the site via a mobile url get redirected to the non mobile url on this example;
user visits http://website.com/*mobile/*the-page.html
redirect them to http://website.com/the-page.html
Doing a google search, I was able to come up with the follow htaccess line:
RewriteRule ^/?mobile/(.*)$ /$1 [L,R=301]
though it correctly redirects to the right page, it uses the filename of the dynamic page to do so, example:
http://website.com/index.php?id=the-page
how can I get it to just take out mobile/ from the url?
okay, slow moment... I needed to deleted the original rewrite rules for the mobile site.
after that, it was simple as adding
RewriteRule ^mobile/(.*)$ /$1 [L,R=301]
to htaccess.

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.

UrlRewrite & Redirect with an optional /XX/home part in Apache2

For a perfect working and Error404-Free log file, I have to redirect all previous (now wrong) home page urls to the new homepage. For that I have a partially working rewrite:
RewriteRule ^home$ /en/aster [R=301]
// Works for site.com/home but NOT for site.com/en/home or site.com/xx/home etc.
Q1. How to make an optional part ?????/ sothat site.com/anything/home permanently redirects to site.com/anything/home making sure that the root is not another folder but the actual real sites root followed by the two language chars then /home?
Q2. for consistency and SEO purposes, Do I need a [R=301] or a [R=301,L] ?
Your ideas and help is highly appreciated.
Not sure if I completely understand what you want the site.com/anything/home to redirect to, but this is how I understood:
RewriteRule (.*)/home $1/en/aster [R=301,L] Will match all requests ending in "/home". The (.*) will keep the urls in the same path. Therefore site.com/anything/home redirects to site.com/anything/en/aster
As far as SEO, there is no difference between [R=301,L] and [R=301]. The L informs mod_rewrite to not process any further rules, which in this case is what you would want, since you are redirecting to a new page.
Hope this helps.