redirect ALL urls from old site to index.php on new one with .htaccess? - .htaccess

A couple questions, simply:
Is the .htaccess file generally stored in the public_html directory? I think so, no?
If I do not find such file, can I simply create it and upload it with FTP?
3 (most importantly). What is the code I need to redirect ALL URLS to ONE new URL, namely, http://www.newsite.com
Is it nothing more than Redirect 301 / http://www.newsite.com/?
Thanks

Is it nothing more than Redirect 301 / http://www.newsite.com/?
Correct, it is the only thing you need. It will redirect anything starting with / to the appropriate place in http://www.newsite.com/. Example:
You go to http://oldsite.com/some/path/to/file.php, you'll get redirected to http://www.newsite.com/some/path/to/file.php.
If you want everything to go simply to the document root of the new site, you can use a RedirectMatch instead:
RedirectMatch 301 .* http://www.newsite.com/
So if you go to http://oldsite.com/some/path/to/file.php, you'll get redirected to http://www.newsite.com/
Not all apache installations come with mod_rewrite installed, if it's not installed you'll get a 500 server error if you attempt to use the rewrite engine. However, mod_alias is usually always installed.

Question 1: Whether you can just upload .htaccess to public_html depends on the web host, but that will likely work.
Question 2: I would set up the redirect like this:
RewriteEngine On
RewriteRule ^(.*) http://www.newsite.com/ [R=301,L]

Related

How do I redirect a web URL using .htaccess

I want to redirect users who enter this website URL:
http://www.myWebsite.com/bananas
to:
http://www.myWebsite.com/fruits/bananas
I cant test it because I'm sending this to somebody.
I have these but I don't know for sure which one works:
RedirectMatch 301 http://www.myWebsite.com/bananas(.*) http://www.myWebsite.com/food/bananas $1
Options +FollowSymlinks
RewriteEngine on
rewriterule ^bananas(.*)$ http://www.myWebsite.com/food/bananas $1 [r=301,nc]
Please specify if you want to redirect or rewrite. The rules you are using serve different purposes and you used both in your example.
Redirect: Actually load a different site when entering the url (end up at url and content of /fruits/bananas)
Rewrite: Url stays the same but server provides rewritten content (url stays at /bananas, but show /fruits/bananas content)
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
Also it is not clear if you only want one single directory to be redirected or all files that are contained in that directory.
Checkout this as a guide: http://www.htaccessredirect.net/
I believe you are looking for
Redirect 301 /bananas http://www.myWebsite.com/fruits/bananas
The HTTP 301 stands for Moved Permanently.
I haven't tested it, though.

Advanced 301 redirect for an entire site at site root?

I am struggling with an age old problem. I inherited a site with some pretty good SEO and one glaring problem. The entire site is hosted on the /site/ subdirectory. I have decided that I need to load the site at the root. So something like http://example.org/site/index.php will instead redirect to /index.php (<-- that counted as a link, if it is unclear I mean it to be the root of the site/index.php.)
We use joomla for our backend and there are hundreds of pages on the site at this point. I have struggled getting any of the redirects I have seen to do what I want them to do. Basically, any page our patrons visit from an old link with /site/ in it should be redirected to the exact same link, but without the star.
I am open to just loading the page from /site/ and making it look like it is from root. It is my understanding that this can be done with some advances mod-rewrite (http://kb.mediatemple.net/questions/85/Using+.htaccess+rewrite+rules#gs ?) but I have not had any success yet. I run a beta site that mimics the parent site in a subdomain that I have already moved from /site/ to / so I can test a lot of .htaccess configs.
Any help is appreciated... thanks!
Just to be sure: you want http://example.org/site/foo/bar/baz.php to go to http://example.org/foo/bar/baz.php, that is, to remove (via redirect) the /site prefix if it's there, but not touch the URL otherwise, right? If so, it depends on which server you're using:
If your server is Apache, you could use something like this in .htaccess:
RedirectMatch 301 ^/site/(.*)$ http://example.com/$1
If it is nginx, add this to the server {...} session of your site's file (usually symlinked inside /etc/nginx/sites-enabled):
location ~ ^/site/(.*)$ { rewrite ^/site/(.*)$ /$1 permanent; }
Here is a good explanation on how such pattern-based redirects can be set up in both servers.
This seems to be the working answer that I will go with. Basically, this needs put into the directory that you are wanting to redirect from, in my case, that was root/site/.
RewriteEngine on
RewriteCond %{http_host} !^www.beta.example.org$ [nc]
RewriteRule ^(.*)$ http://beta.example.org/$1 [r=301,nc,L]
I assume the first rule ignores www? I'd love to work around that but am not sure exactly why it would have been created anyway. this will rewrite any URL that accesses that .htaccess file (inside your subdirectory) and direct you to the same URL without the subdirectory listing. It doesn't really work with the index.php rewrite tool, but that is fine because it still reaches the correct page.
If anyone has a better option for me with use with Joomla I would be glad to hear it. But, I tihnk this is what I will go with for now because it is giving me great results.
if your server is apache2, you can configure there
<VirtualHost *:80>
ServerName sitio.com
ServerAlias www.sitio.com
DocumentRoot /home/user/public_html/sitio/
...
</VirtualHost>
You need permissions for this change

Redirect directory to a subdomain

I have my domain pointed to a Rails app in Heroku, so for example, when you wanted to access some video, you would have an URL such as:
http://codigofacilito.com/video/W53XHWkbz34
We need to change that app to a subdomain, so the updated version would be:
http://videos.codigofacilito.com/video/W53XHWkbz34
What I want to know is:
Is there a way to redirect people to the new url with the subdomain videos by using the .htaccess file.
If you need this rule to only be applied to the video route, then give the following a try:
In your document root's .htaccess file, insert the following rule:
RewriteRule ^(video/.*)$ http://videos.codigofacilito.com/$1 [R=301,L,NC]
This works for me
RedirectMatch 301 ^/xxx/(.*)$ http://xxx.domain.com/$1
Using mod_alias:
Redirect permanent /video http://videos.codigofacilito.com/video
The keyword permanent causes Apache to send an HTTP status of 301 Moved Permanently instead of 302 Found.

trying to redirect a link in joomla or .htaccess

we moved our joomla site and rebuilt. in the process a link got moved that we need to be as it was before.
before:
www.mysite.org/kindergym
now it lives here:
www.mysite.org/education/kindergym
it would seem that it would be easy to go into com_redirect and do this. however, it only works for the following
mysite.org/kindergym without the www
with the www attached writing the old url returns a 404 error page, not a redirect.
i tried to make a separate redirect with the www too and it wouldnt let me. i tried a separate module with no success and have played around with the .htaccess file (although i am not very knowledgeable about htaccess).
could someone explain the reason why this would be an issue? the difference between the two. i tried calling my host and they were less than helpful and actually told me what i wanted to do couldnt be done LOL.
thanks.
I take it the solution you have would work if you redirect the entire mysite.org to www.mysite.org?
If so, create a .htaccess file in the website root. Put the following inside it:
########## Begin - Redirecting non-www request to www
#
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite.org [NC]
RewriteRule (.*) http://www.mysite.org/$1 [L,R=301]
#
########## End - Redirecting non-www request to www
You also need to make sure mod_rewrite is enabled on the apache-server, but I think most providers support that.
I suggest you post your full .htaccess here. However I think all you need is this rule:
RewriteRule ^(?!education/).*)$ education/$1 [L,NC]
The other two answers are good! but better implement 301 redirect in httpd.conf since it's compiled once on server restart. The same code in .htccess is interpreted for each and every HTTP request!

mod_rewrite + htaccess

I have a (Joomla) site that is not currently pointing to a domain, but is sitting directly on my server IP (i.e.: http://176.158.165.1/~sitename)
Whenever I try to test my mod_rewrite, I get 404 errors. I have tested on another site on the same server, but it works 100%. Could this problem be due to the direct link to the IP address?
My .htaccess file is as follows:
RewriteEngine On
Options +FollowSymLinks
Redirect /google.html http://www.google.com
Thanks,
Dev
try:
Redirect 301 /google.html http://www.google.com
Try #2: (using mod_rewrite)
RewriteRule ^google\.html$ http://www.google.com [R=301,NC,L]
When working in a subdirectory, you need to add the directory name regardless where the htaccess file is placed:
Redirect /~sitename/google.html http://www.google.com
you can also use RewriteBase but to be totally honest, I never really figured out how to get that working, and I've been too lazy to sort it out.

Resources