htaccess redirect folder to new domain index only - apache

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.

Related

Apache - redirect only the domain URL to a subpage

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]

htaccess redirect without .php extension

I recently changed a directory /old_dir/ to be /new_dir/ using this:
RedirectMatch 301 /old_dir/(.*) /new_dir/$1
Which seems to be working perfect for the url:
http://www.mysite.com/old_dir/test.php?var=xxxx
goes to
http://www.mysite.com/new_dir/test.php?var=xxxx
where test.php is the filename. But in many places I use:
http://www.mysite.com/old_dir/test?var=xxxx
which comes up with:
The requested URL /old_dir/test was not found on this server.
not using the .php extension takes advantage of some sort of apache plugin that knows it's a php handler, which seemingly messes up redirect because it says it doesn't exist now.
I am not sure how to fix this issue.
Edit: All the solutions are for this special case, but note that i have about 1000 other files that may not be php, or named the same.
For right now I just made a symbolic link in the old_dir with the name "test" to point to the new_dir's test.php. But I am still looking for a non-specific solution that includes my scenario.
Have you ever tried using mod_rewrite?
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)$ $1/$2.php [QSA]
RewriteRule ^old_dir/([^/]+)/$ new_dir/$1.php [QSA]

How do I rewrite www.sitename.com/thing/thing.php?otherthing=something-like-this to www.sitename.com/something-like-this?

How do I rewrite
www.sitename.com/thing/thing.php?otherthing=something-like-this
to
www.sitename.com/something-like-this?
please help me with this as I can't seem to succeed. My host uses apache 2.2. Many thanks for your help!
Update
No I don't need that trailing ? However, I used the Rewrite rule you offered me and it still ain't working. I also added a RewriteEngine On before the rules.
I have Linux hosting, .htaccess and the code is obviously semantically correct, cause otherwise I would get the all so popular 500 internal server error. I placed the .htaccess file in the folder thing and in the root of the site, but it still won't work.
There should be an option to display it in directory format instead of the PHP ? format. If not, you could use the .htaccess mod_rewrite rule to make that display in the /folder/ way.
The way I do it is that I just upload my files and each page name is index.html and then I create folders, and put each index.html in the folder. Like this:
/guidelines/
In that folder is index.html, so instead of it being /guidelines.html it's /guidelines/
Looks better without .html
You need to use mod_rewrite:
RewriteCond %{QUERY_STRING} ^otherthing=(.*)$
RewriteRule ^thing/thing.php$ /%1? [L]
No idea if you meant to have that trailing ? at the end of the rewrite, I don't think that's possible. Note that the ? at the end of the RewriteRule is to get rid of the query string, otherwise, the rewritten URL will still have the ?otherthing=something-like-this at the end.

apache .htaccess - cut a string from url and redirect

For some reason google indexed several pages of my website as:
http://myapp.com/index.php/this-can-be-enything/1234
Now, I want to redirect with apache .htaccess those pages to correct urls:
http://myapp.com/this-can-be-enything/1234
I've googled and tried many options but with no success.
Any tip will be helpful.
I've added to my .htaccess file following lines:
RewriteCond %{THE_REQUEST} ^.*index.php.*
RewriteRule ^(.*)index.php(.*)$ $1$2 [NC,R=301,L]
I don't know if this is best solution but works ok for me.
Two Parts of problem
To make Google aware that indexed page is moved to some other destination you need to handle that # apache level and issue 301 ( moved permanently )
Handler to handle the cached requested URL to new URL using the #1 handler itself.

URL rewriting that visibly rewrites (changes the URL in the address bar)

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/