Htaccess 301 redirect htaccess or php - .htaccess

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.

Related

Wildcard RewriteRule for 301 redirects in htaccess

I'm working on setting up a 301 redirect in an .htaccess file and I can get it to sorta work, but I'm not sure why it's not picking up on anything after a specific URL in the rewrite rule.
I currently have this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/old-path/page/(.*) /new-page/ [R=301,L]
</IfModule>
Which works fine if I test it with http://example.com/old-path/page which redirects to http://example.com/new-page
But if I try this:
http://example.com/old-path/page/test it will 404 and not redirect to http://example.com/new-page
Overall, since I'm not 100% if there are any other pages from the old path that may be nested within the page I want to make sure that I'm catching any of them and just redirecting to the new-path outright.
So, I'm not sure if I'm setting this up incorrectly or if there is something that I'm missing?

htaccess 301 redirect not working for new subdomain site

I'm moving my site so wanted to test a page and redirect in the htaccess file:
I wanted to redirect
http://www.martinspencephotography.co.uk/blog/yes/mini-photo-series-minimalism-gallows-hill-outside-cloughmills
to
http://landscape.martinspencephotography.co.uk/minimalism-at-gallows-hill/
using the following in the .htaccess file:
Redirect 301 /blog/yes/mini-photo-series-minimalism-gallows-hill-outside-cloughmill http://landscape.martinspencephotography.co.uk/minimalism-at-gallows-hill
It didn't works there any reason why this might not work?
Try
RewriteRule your_page.html
http://your.url.fr/yournewpage.html [R=301]
And what about
http://www.martinspencephotography.co.uk/blog/yes/mini-photo-series-minimalism-gallows-hill-outside-cloughmills http://landscape.martinspencephotography.co.uk/minimalism-at-gallows-hill/ [R=301]
So the answer was to use DRUPAL NODE...

.htaccess 301 Redirect from Old URL structure to New URL structure

I'm trying to redirect a user to a new URL structure.
Here's the old URL structure:
http://www.mywebsite.com/pants/pant-item-1234
Here's the new URL structure:
http://www.mywebsite.com/catalog/pants/pant-item-1234
Any idea how to do this with an HTACCESS 301 redirect?
RewriteRule ^pants/(.+)$ /catalog/pants/$1 [R=301,L]
Or, if pants can actually be something else
RewriteRule ^([^/]+)/(.+)$ /catalog/$1/$2 [R=301,L]
I haven't tested it, so I'm not sure it will work, sorry
Just use Rewrite URL... I wouldn't redirect unless you're not using mywebsite.com as a landing page at all. http://httpd.apache.org/docs/2.0/misc/rewriteguide.html You will need to make sure you have your rewrite engine on.

How to do 301 redirects with folders & subfolders?

I need to redirect all the old URLs on my site to new URLs, but am having issues. A given URL and its subfolders need redirecting to the same page.
For example, this is the first one:
redirect 301 /panache/sports-bra/ http://www.newdomain.co.uk/sports-bra.html
This works ok. However there are these size sub-pages which need to redirect to the same location:
redirect 301 /panache/sports-bra/30DD http://www.newdomain.co.uk/sports-bra.html
redirect 301 /panache/sports-bra/30E http://www.newdomain.co.uk/sports-bra.html
redirect 301 /panache/sports-bra/30F http://www.newdomain.co.uk/sports-bra.html
And these don't work, I end up at a location like the following:
http://www.newdomain.co.uk/sports-bra.html30DD
See the way the last part of the path is appended to the url? I'm guessing it's because the second redirect is conflicting with the initial 301 redirect?
I have also tried using this rewrite rule but have had no luck. The site is Magento so I don't know if this has some effect? mod_rewrite is enabled on the server.
RewriteRule ^panache/sports-bra/ http://www.newdomain.co.uk/sports-bra.html [R=301]
Any help mucha ppreciated.
Try this.
RewriteEngine on
RewriteRule ^(panache/sports-bra/.*)$ /sports-bra.html [L,R=301,NC]

301 redirect after url rewrite

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

Resources