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

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>

Related

Problem with redirection of shop subcategories in .htaccess

I couldn't find an answer, how to redirect certain subcategories...
I've old shop with paths like that:
shop.com/old_cat1/old_cat2/old_cat3
What I want, is to redirect it like that:
shop.com/new_cat2
shop.com/new_cat3
When I'm trying to use this code:
Redirect 301 /old_cat1/old_cat2/ https://shop.com/new_cat2
Redirect 301 /old_cat1/old_cat2/old_cat3 https://shop.com/new_cat3
redirection from old_cat2 to new_cat2 is working, but redirection to new_cat3 is sending me to new_cat2 (higher level). What should I do? I was trying with RedirectMatch, but it gave me nothing.

How do I format this htaccess rule to not redirect a specific query?

I am using an htaccess rule to redirect all urls in the format of:
www.example.com/blog/the-name-of-the-post
to
www.example.com/the-name-of-the-post
So I've removed "blog" from the URL. The redirect rule below is working. However I do not want to redirect any URLs in the format of:
www.example.com/blog/page/x
So if "/page" appears after "blog", then I don't want to do the redirect. The problem is I'm also redirecting the blog pages when paginated.
RedirectMatch 301 ^/blog/([^/]+)/([^/]+)/$ https://www.example.com/$2
In sum,
www.mysite.com/blog/the-name-of-post redirects to www.example.com/the-name-of-post
www.example.com/page/1 (or page/2, page/3, etc) do not redirect
Thanks
If you don't want to redirect when page appears after blog, you can do a negative match for the word page, like so:
RedirectMatch 302 /blog/(?!page).*$ https://www.example.com/
I'm not entirely clear on what all the different possibilities are from your description so I'm just giving you an example of not matching when the word page appears in the second position of the URI.

htaccess redirect with folder name

I need some help with htaccess redirects. Out site was wrongly crawled by google and as a result there are a lot of wrong urls being shown in webmaster tool. As an example:
articles/abcd/xyz
should be redirected as
articles/abcd/
articles/abcd/xyz.php
should be redirected as
articles/abcd/
articles/abcd/xyz.html
should be redirected as
articles/abcd/
So basically I am trying to mean always redirect to articles/abcd/ for varous wrong url types that i shown above. Please could you help
You can simply use RedirectMatch from mod_alias (documentation). We assume that the part after articles does not contain any / character. We redirect with a temporary redirect to the url without the suffix. Change "temp" to "permanent" after testing that this redirect actually works as you expect it to work.
RedirectMatch temp ^(/articles/[^/]+/).+$ $1

htaccess Redirect to new url

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/

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

Resources