htaccess Redirect to new url - .htaccess

I have below url
www.site.com/Photo/Actor/ActorName/GalleryName/2
Here
Photo and Actor are static strings in url
ActorName and GalleryName's are dynamic
2 is the photo index..
How can i Redirect this url patten to the following
www.site.com/Actor/ActorName/GalleryName/Photo/2
I am changing my site url format.
my site is already well crawled and well indexed in google so i want to redirect the urls to new format so currently indexed urls do not die.

You could dynamically rewrite it as follow:
RewriteRule ^(Photo)/(Actor)/([^/]+)/([^/]+)/([0-9]+)$ /$2/$3/$4/$1/$5 [R=302,NC,L]
To further explain it:
^(Photo)/(Actor)/([^/]+)/([^/]+)/([0-9]+)$
This means, if it starts with Photo/Actor/ActorName/GalleryName/Numbers.
Then redirect to Actor/ActorName/GalleryName/Photo/Numbers using a 301 permanent redirect.
The /$2/$3/$4/$1/$5 means the reordering of the above mentioned.
Keep in mind I am using R=302 on the above code and you should also use R=302 until you confirm it is fully working to your needs, then you can safely switch it to R=301.
This is to prevent your browser from getting cached with previous attempts so you can see whether its working or not until make it definitive.
If you already done previous attempts, I suggest you to use a different browser to test the URL preferable one that you haven't used to access the site to make sure its not accessing cached information.

RewriteRule ^Photo/Actor/([^/]+)/([^/]+)/(\d+) Actor/$1/$2/Photo/$3 [L,R=301]

You can use:
<?php
header("Location: http://www.mydomain. com/new-page.html", true, 301);
exit();
?>
To know more: http://prowpexpert.com/

Related

How to make 301 redirect from one website to another where the ending is different?

I have 2 websites:
OLD one - https://www.old.example/en/
NEW one - https://new.example/en
Lastly, Google Search Console reported around 80 improperly redirected links for OLD website, i.e.:
https://www.old.example/en/?p=41310
https://www.old.example/en/?p=45659
https://www.old.example/en/?p=72785
In .htaccess of OLD page is inputted only code:
Redirect 301 / https://new.example/
which redirects above links from OLD page to i.e.
https://new.example/en/?p=62692
How can I correct it and i.e. expect to have in such cases always redirection to main page - https://new.example/en
To remove the query string completely (without a stray ? at the end) you'll need to use mod_rewrite instead.
For example, in the .htaccess at the old domain:
RewriteEngine On
RewriteRule ^(en)/ https://new.example/$1 [QSD,R=301,L]
Aside: Although this many-to-one redirect will likely be seen as a soft-404 by Google.

.htaccess rewrite url that has already been changed

I am upgrading my site which involves new scripts and a different URL
structure. There are currently a few thousand pages so I want to
basically move them in to a subdirectory so that they are not lost.
I am not very confident with htaccess so can someone please confirm that
the first part I have done is correct:
Old URL: http://www.example.com/article/another-dir/page-group/whatever.html
RewriteRule ^article/?$ http://www.example.com/archive/ [R=301,NC,L]
To achieve this: http://www.example.com/archive/another-dir/page-group/whatever.html
Search engines will see the above as a permanent move and in the address bar
it will show the new url. Is this right?
The second part is on the new script - the url's could be improved but I am
unable to change all the script so thought about using htaccess again but am
not sure if it can be achieved like this.
At the moment the url looks like this:
url: http://www.example.com/category/4/categoryname
In the htaccess the current rewrite rule for this type of url looks like this:
RewriteRule ^category/(.*)/(.*)$ category.php?id=$1&slug=$2
Is it possible to change this so that in the url address bar I end up
with this:
http://www.example.com/categoryname
Basically I don't want to see either the number or category/ in the resulting
url as it will be easier for visitors to use - is that possible??
Thanks in advance for any help.
The second question related to passing in URI components for querystring redirect and then hiding those components in the URL I don't think would be easy, if even possible, using RewriteRules.
For the first question though. Given the sample URLs you mentioned, the RewriteRule would need to include capture and backreference if you want to preserve the full URL in the redirection. For example:
RewriteRule ^article/?(.*)$ http://www.example.com/archive/$1 [R=301,NC,L]

htaccess redirecting from rewritten dynamic urls to new dynamic urls

i am experiencing a very unique problem and i hope someone can help!
so we have recently created a new ecommerce website and we made it live and everything was working great but when we to implement our 301's from our old pages we were getting some wierd things
so the code below actually works
Redirect 301 /directory/ http://mysite.com/index.php?cat=1
this code does not
Redirect 301 /directory/sub_directory/ http://mysite.com/index.php?cat=2
the output when i try to do this redirection is "Invalid parameters specified!" on a blank webpage and in the address bar it has this
http://mysite.com/index.php?cat=1/sub_directory/
we were thinking that maybe the problem is because our old pages were dynamic but mod_rewrite was used to create more readable urls and we have also deleted all our old files because they were interfering with our new pages rendering
any help would be greatly appreciated!
thanks
That is strange, as redirect should only match the specific url listed, where as it looks like its behaving like rewriterule and partially matching the subdirectory url against the first rule..
try putting the more specific rule above the less specific, like so:
Redirect 301 /directory/sub_directory/ http://mysite.com/index.php?cat=2
Redirect 301 /directory/ http://mysite.com/index.php?cat=1
That way the more specific rule will be hit first, and the /directory/ only rule will only match if more specific matches above fail
alternatively, you could try RewriteRules:
RewriteRule ^directory/$ http://mysite.com/index.php?cat=1 [R=301,NC,L]
RewriteRule ^directory/sub_directory/$ http://mysite.com/index.php?cat=2 [R=301,NC,L]
the ^ and $ anchors should prevent any unwanted partial matching

using mod_rewrite to create SEO friendly URLS

I've been searching google for this but can't find the solution to my exact needs. Basically I've already got my URL's named how I like them i.e. "http://mysite.com/blog/page1.php"
What I'm trying to achieve (if it's possible!) is to use rewrite to alter the existing URLS to: "http://mysite.com/blog/page1"
The problem I've come across is I've found examples that will do this if the user enters "http://mysite.com/blog/page1" into the broweser which is great, however I need it to work for the existing links in google as not to loose traffic, so incoming URLS "http://mysite.com/blog/page1.php" are directed to "http://mysite.com/blog/page1".
The 1st example (Canonical URLs) at the following is pretty much what you want:
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html#url
This should do the trick, rewriting requests without .php to have it, invisible to the user.
RewriteEngine On
RewriteRule ^/blog/([^.]+)$ /blog/$1.php
You will need to write a rewrite rule for mapping your old url's to your new url as a permanent redirect. This will let the search engine know that the new, seo friendly url's are the ones to be used.
RewriteRule blog/page1.php blog/page1 [R=301,L]

how to use htaccess to chance the URL...?

I googled and looked on stackover flow but i failed to really understand the answers
I;m not trying to REDIRECT but CHANGE the WAY the URLS LOOK
I want to change a these into the new urls
www.site.com/abc.php to www.site.com/
(my index page currently has my login page so I can't use the index.php)
www.site.com/abc.php#123.php to www.site.com/123.php
UPDATE:
Ok, how can I do this then
www.site.com/abc.php to www.site.com/abc/
so that when a hash link is present it looks like
www.site.com/abc/#123.pho
or if possible
www.site.com/abc/#123/
In addition to the other answer on redirects, if you want more advanced forwarding you can use the Apache RewriteEngine module.
You can then use regexps, which may include subpatterns.
Example from my site, I created the patterns after I've imported everything from blogger to wordpress. Whenever someone visits an URL like http://www.twistedmind.nu/2006_03_01_archive.html he'd be redirected to http://twistedmind.nu/2006/03
RewriteEngine on
RedirectMatch 301 (([0-9]*)_([0-9]*)_([0-9])(.)(.html))$ http://twistedmind.nu/$2/$3/
RedirectMatch 301 (([0-9])(.)(.html))$ http://twistedmind.nu/$1
Based on the other answer, you can't match on everything that's after the hash tag # though.
Other example (added after comment):
RedirectMatch 301 (.*).php$ http://www.mysite.com/$1
This should strip the .php extension from all links, the new link (withouth .php) should exist.
You can use mod_rewrite if you want to create 'virtual' urls that redirect something like mysample to mysample.php. See http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html for an explanation.
You can use an .htaccess only for the first url.
Redirect /abc.php http://www.site.com/
The second url cannot be redirected with the .htaccess.
You can use javascript for that:
<script type="text/javascript">
if (location.hash == "123")
location.href = location.hash+".php";
</script>

Resources