Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 220
Daystar1124 Avatar asked Oct 19 '22 23:10

Daystar1124


1 Answers

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.

like image 178
chesterbr Avatar answered Oct 24 '22 01:10

chesterbr