Advanced 301 redirect for an entire site at site root? - .htaccess

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

Related

URL masking (?in .htaccess) from one domain to another

I've been searching the archives but I can't find anything that is making too much sense to me.
I have a site with a couple of subdomains which redirect to other sites.
E.g.
the visitor types - www.jmp.redtwenty.com.au - and is redirected to - http://creator.zoho.com/redtwenty/jmp-conversion-tracking
Is there any way to mask this redirect so that the visitor still sees jmp.redtwenty.com.au in the address bar?
I keep seeing mention of a rewrite rule in .htaccess but not sure if that is what I want.
Thanks
Mike
You can do this a few ways, but you'll need to make sure mod_proxy is enabled.
If you have control of the server config or the vhost config of the www.jmp.redtwenty.com.au/ domain, you can add this to it:
ProxyPass / http://creator.zoho.com/redtwenty/jmp-conversion-tracking/
Or in the htaccess file in the document root of http://www.jmp.redtwenty.com.au/:
RewriteEngine On
RewriteRule ^(.*)$ http://creator.zoho.com/redtwenty/jmp-conversion-tracking/$1 [L,P]

.htaccess rewrite full domain name

Are there anyway to make a user get to download thefile by let them type:download.mywebsite.com/thefile
in the browser address while the "download.mywebsite.com/thefile" does not exist but the "www.mywebsite.com/thefile" does.
I've got nearly zero knowledge with .htaccess and mod_rewrite engine. I hope my question is clear.
IF you own mywebsite.com then yes you could, you would have to place a ServerAlias in your root httpd.conf directive then use mod_rewrite to re-write all requests for download.mywebsite.com/thefile to www.mywebsite.com/thefile but the mod_rewrite would not know if a file exists or not in advance.
Another way to do it would be implement a custom error document for a 404 handler which redirects a user to a suggested URL.
RewriteEngine On
RewriteRule ^/download/(.*)$ http://download.mywebsite.com/$1 [R=301,L]
You need to set up your web server to listen for sub-domains as well

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!

htaccess selective redirect

I am trying to redirect all sub-directory pages to main directory, except of a few pages (e.g. (somepage1.html).
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(de|ru)/somepage1.html
RewriteRule ^([a-z]{2}|zh-CN|zh-TW)/(.*)$ /$2 [R=301,L]
Everything working except de/somepage1.html is redirected to home page (/), which is not acceptable. I wont it not redirected at all.
How can I achieve it
Thanks1
Well above rules are clearly excluding de/somepage1.html URL so it is most likely some other rule that is redirecting de/somepage1.html to /. Are you using wordpress or some other CMS tool by any chance? That might have its own rules in .htaccess file, check that please.
Also it would help to check web server's access log when this redirection happens.
the code I provided is working perfectly, so if somebody looking for this kind of a solution can use it without a fought.
The reason for not working is that my website were keeping cash and therefore was not renew frequently.

Need to redirect to true root folder

I am running a website on MAMP, and the root is http://localhost/sandbox
When I have links that link to, for example - /calendar
it directs them to localhost/calendar, I want it to redirect to localhost/sandbox/calendar
What would I have to do in htaccess to get it to redirect everything to localhost/sandbox/ as the root?
You would usually change the application or site that generates the links, and have that add the /sandbox to the URL.
If that's not possible, putting this into the web root directory (the one above /sandbox) should do:
RewriteEngine on
Options +FollowSymlinks
RewriteCond %{REQUEST_URI} !^/sandbox # If URL does not start with /sandbox...
RewriteRule (.*) /sandbox/$1 # Add /sandbox/ in front of it
(if it's easier to achieve with Alias, Apache Gurus, feel free to add your solution, but I couldn't get Alias to work for this scenario.)
However, this solution will make any other directory besides /sandbox inaccessible. You may want to re-think your Virtual hosts structure!

Resources