301 redirect after url rewrite - .htaccess

I did some htaccess URL rewrite. To keep my google ranking I must redirect the old URL to the new one; the problem is the old URL still 'exist' and I'm not sure how to do the redirect. This is an example:
old url: mypage.php?id=myId
which now is rewritten as: mypage-myId.html
this is the htaccess directive
RewriteRule ^mypage-([A-Za-z0-9_-]+).html$ mypage.php?id=$1 [L]
now I want to 301 redirect all the old url (mypage.php?id=myIds) to the new url (mypage-myIds.html).
I tried this at the top of my htaccess file:
redirect 301 mypage.php?id=1 to mypage-1.html
but nothing happens, the page stays on mypage.php?id=1.
What's wrong with this? I found another post about this problem
url rewrite & redirect question
but the solution wasn't that clear to me.
Thanks in advance
Vittorio

You could try this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([A-Za-z0-9_-]+)$ # fetch ID
RewriteRule ^mypage\.php$ http://domain.com/mypage-%1.html [R=301,L] # redirect old URL to new
RewriteRule ^mypage-([A-Za-z0-9_-]+)\.html$ mypage.php?id=$1 [L] # rewrite

Related

htaccess to remove trailing /login from URL

For some reason Googlebot are crawling URLs with a "/login" at the end.
I'm trying to remove this trailing /login , 301 redirect with htaccess.
domain.com/login - this URL exists
but these do not :
domain.com/*/login domain.com.au/*/*/login domain.com.au/*/*/*/login
etc
Following is htaccess rules tried:
RewriteEngine ON
RewriteRule (.*)/login$ /$1 [L,R=301]
Please try following htaccess rules. Please make sure to clear your browser cache before testing your URLs. As per description, this will redirect all urls which are ending with login and NOT starting from login.
RewriteEngine ON
RewriteRule ^(.*)/login/?$ $1 [R=301,NC,L]

htaccess redirect some partical url 301 SEO

something on our BLOG URLS have changed.
No i want to have some URL path deleted in urls.
Example:
OLD URL:
https://do-main.de/blog/entry/die-xxx-der-portrait
NEW URL:
https://do-main.de/blog/die-xxx-der-portrait
My htaccess attempt to setup:
#OLD BLOG URLS
RewriteEngine On
# anything that is equal to https://do-main.de/blog/entry/*
RewriteCond %{HTTP_HOST} ^do\-main\.de\/blog\/entry\/$
# redirects to https://do-main.de/blog/*
RewriteRule ^/?(.*)$ https://do-main.de/blog/$1 [R=301,L]
conclusion all URLS with "entry" should be only using /blog/ without /entry/
Do you understand? Can anyone help?
You can use Redirect directive.
Redirect 301 /blog/entry/ http://example.com/blog/
This will 301 redirect all requests from example.com/blog/entry/foobar to http:// example.com/blog/foobar .

Htaccess 301 redirect htaccess or php

I have a rewrite rule in Htaccess as below for a dynamic URL
RewriteRule ^cartoon-([^-]*)-([^-]*)\.html$ /fm
/cart_new?r_id=$1&location=$2 [L]
This rule results into URL as http://localhost/fm/cartoon-34-singapore.html
Now my client want to change this URL to http://localhost/fm/singapore/34/goofie and i wrote .htaccess as
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ /fm
/cart_new?location=$1&r_id=$2&cartooname=$3 [L]
The above rewrite is working fine but client wants that all OLD URLs like i.e.http://localhost/fm/cartoon-34-singapore.html shall 301 redirect to http://localhost/fm/singapore/34/goofie.
This is driving me crazy. I have tried various things but none seems working.
In your PHP code you can do something like this for redirection:
header ('HTTP/1.1 301 Moved Permanently');
header("Location: {$rebuilt_url}");
Here $rebuilt_ur will be set to http://localhost/fm/singapore/34/goofie by querying your database.

301 redirection in htaccess

I am creating htaccess for my site I need to redirect old urls to new url through 301 redirect. I have created code in htaccess as follows
My old urls like
www.example.com/categories/city/cityname/brandname/product1.html
and my new url is like
www.example.com/product1.html
For this scenario I have written following code in htaccess
RedirectMatch 301 ^/categories/city/cityname/(.*)$ http://www.example.com/$1
Please help me regarding this scenario or where I am doing wrong.
Try using mod_rewrite's functionality in your .htaccess like this:
RewriteEngine On
RewriteRule ^/categories/city/cityname/(.*)$ /$1 [R=301,L]
Referring to #Seybsen answer, this 1 line should fit all your needs :
RewriteRule ^/([a-zA-Z]+)/([a-zA-Z]+)/([a-zA-Z]+)/([a-zA-Z]+)/(.*)$ /$5 [R=301,L]

How to redirect these old urls to new ones (subfolder) via htaccess

I am a newbie and would like to redirect all my old urls to new ones (moved as subfolder) via htaccess. Also, with 301 permanent redirect.
Old url pattern looks like:
http://www.mydomain.com/de/myoldpage.html
New url pattern should be:
http://www.mydomain.com/shop/de/myoldpage.html
I am using the same domain and same page urls but only my shop is moved to a subfolder "shop".
How can i write a redirect rule to redirect all the urls in this pattern.
Thanks in advance.
Try this code in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteRule ^(de/.*)$ /shop/$1 [L,R=301,NC]
RewriteEngine On
RewriteRule ^de/(.*)$ shop/de/$1 [L,R]

Resources