I asked sort of the complement of this question before:
Mod_rewrite invisibly: works when target is a file, not when it's a directory
Now I actually want a rewrite to happen visibly, because I've switched URL schemes and although I want the old links to work, I want the user to see the new URL scheme.
So this works
RewriteRule ^oldscheme/(.*)/?$ newscheme/$1
But the URL in the address bar remains as http://example.com/oldscheme/foo.
What's the right way to do a visible rewrite, preferably just with mod_rewrite as opposed to something kludgy with Location redirects or somesuch?
As I cannot leave comments now, I'll post my addition to Ignacio's comment here.
You actually should post a 301 (Moved Permanently) redirect, as you're describing there's a new site directory structure. So your RewriteRule should read
RewriteRule ^oldscheme/(.*)/?$ newscheme/$1 [R=301]
It turns out adding a "redirect" code does the trick:
RewriteRule ^oldscheme/(.*)/?$ newscheme/$1 [R]
Obvious in retrospect, but hopefully this makes the answer more searchable.
I found it on this excellent "cheat sheet":
http://www.addedbytes.com/cheat-sheets/mod_rewrite-cheat-sheet/
Related
I have a website, let's say fruit.com, and currently I have a bunch of redirects set up that work just fine, so for example fruit.com/apples/mcintosh will redirect to fruit.com/apples.php?id=mcintosh.
I also used to have some redirects set up to allow me to use a short URL, so fru.it/mcintosh would redirect to fruit.com/apples.php?id=mcintosh.
So far so good. A few years ago, though, my short domain lapsed and I didn't renew. Recently I've purchased it again and I'm interested in getting the same setup back.
Now, though, the redirects from the short domain to the main domain aren't working, although I've used exactly the same code, so I'm at a bit of a loss for what's going wrong.
RewriteCond %{HTTP_HOST} ^www\.fru\.it$
RewriteRule ^([0-9]+)$ "http\:\/\/www\.fruit\.com\/apples.php?id=$1" [R=301,L]
although I've used exactly the same code
But the code you've posted won't redirect the stated example URL fru.it/mcintosh, since the code matches digits only, not letters.
Try the following instead:
RewriteCond %{HTTP_HOST} ^www\.fru\.it
RewriteRule ^(\w+)$ http://www.fruit.com/apples.php?id=$1 [R=301,L]
The \w shorthand character class matches upper and lowercase letters, numbers and underscore.
You don't need all the backslash-escapes in the substitution string.
Also bear in mind that the order of these directives can be important. This rule would likely need to go near the top of the .htaccess file to avoid conflicts.
Test first with a 302 (temporary) redirect to avoid potential caching issues. Clear your browser cache before testing.
Aside:
fruit.com/apples/mcintosh will redirect to fruit.com/apples.php?id=mcintosh
It would seem to make more sense that this would be a (internal) "rewrite", not a (external) "redirect"? The shortcode would then redirect to fruit.com/apples/mcintosh, not fruit.com/apples.php?id=mcintosh?
I have looked and looked and cant find the answer, I would greatly appreciate your help!!
I designed a website in a folder on a dummy domain, and forgot to add "noindex" and now its indexed, I need to redirect all pages in that folder to the index of the new domain.
example:
http: //dummysite/clientsfolder/
(I had to put space here because I can't post 2 links)
redirect to http://clientsnewdomain.com
all the code I have found redirects to http ://clientsnewdomain.com/clients folder, whether I place it in the /clientsfolder or the http://dummysite/
and then this results in a 404 page. Got into a mess here.
Also which is better to use to avoid this issue in the first place?
in
or a robots.txt?
Use that, in your /clientsfolder/.htaccess:
RewriteEngine on
RewriteRule ^(.*)$ http://clientsnewdomain.com/$1 [R=301,L]
OR in the root .htaccess:
RewriteRule ^clientsfolder/(.*)$ http://clientsnewdomain.com/$1 [NC,R=301,L]
The best place to avoid this issue in the first place is to use robots.txt.
But I prefer to use a folder protected by a password.
Sorry if this has already been asked here, but I haven't found it through looking yet.
I've been asked to remove the /page on our site, and just make it go to the homepage instead. This is easy enough to do in PHP, but I'd rather use my .htaccess file if I can (to learn it better mostly).
In PHP I'd do (some psuedo code)
if($urlIsSlashPage){
header('Location: /');
die();
}
Is something like that possible to port to my .htaccess file?
I did have this,
RewriteRule ^page/$ / [NC,L]
And whilst that did show the home page, the URL in the address bar stayed as /page, I want that to display just / instead.
You can try with a redirect.
RewriteRule ^page/?$ http://www.domain.com/ [NC,L,R=301]
I have a website that recently had a domain name change, and I am trying to set the rules up to include ANYTHING that is after the ruleset to be redirected (sub domains currently work, but file exensions do not) The current ruleset I have in place is:
RewriteRule ^subdomain/(.*)$ http://subdomain.example.com [R=301,L]
If I go to www.example.com/subdomain/ or www.example.com/subdomain/another_directory/ it redirects to the appropriate site, but if I go to www.example.com/subdomain/file.ext or www.example.com/subdomain/another_directory/file.ext, the redirect does not work. I know I need to change the regex so that it works correctly, but i'm not sure what it needs to be changed to.
You are not using captured group in target URL. Use it like this:
RewriteRule ^subdomain/(.*)$ http://subdomain.example.com/$1 [R=301,L,NE,NC]
You don't need mod_rewrite for this.
A simpler, lighter approach would be
RedirectMatch ^/subdomain/ http://subdomain.example.com/
I have a shared hosting Apache server, and I'm trying to send visitors who come to the main domain URL to a specific page, with the URL replaced and a 303 redirect:
example.com
example.com/
to
example.com/subdirectory/page.html
Only the plain domain URL should get redirected, not:
example.com/page.html
example.com/otherdirectory
example.com/otherdirectory/
example.com/subdirectory
example.com/subdirectory/
example.com/subdirectory/otherpage.html
example.com/subdirectory/otherdirectory
example.com/subdirectory/otherdirectory/
example.com/subdirectory/otherdirectory/page.html
I'm not sure that RewriteEngine is allowed on this server, if there are alternative approaches possible.
The crude way I've thought of is to just use DirectoryIndex to send the visitor to example.com/index.php -- and have a PHP redirect in that file go where I want. But I'm not sure if this might produce a visible blip for some visitors, or how Google would feel about it.
I've found other instances of this kind of question on Stack Overflow, but the answers are failing for me in some way or another. As the behavior is not intuitive, testing before posting might be advisable.
Thanks
If mod_rewrite is not enabled you can use mod_alias based rule like this in your DocumentRoot/.htaccess:
RedirectMatch 303 ^/?$ /subdirectory/page.html
UPDATE: Equivalent mod_rewrite rule:
RewriteEngine On
RewriteRule ^/?$ /subdirectory/page.html [L,R=303]