Rewrite URL to access files from directory off root, but not show in path - .htaccess

I would like to run a version of a Wordpress site on my localhost without going through too much hassle of changing url's, etc.
I have figured I will add a line in my hosts file to route domain.com to localhost. I just need a way to route / to look for files in /directory/directory/.
Thanks for any help.

Tried this, didn't seem to do anything:
RewriteEngine on
RewriteRule ^(.*)$ /directory/directory/$1 [L]
I used GasMask to route domain.com to my localhost.

Related

htaccess works with www.example.com but not example.com

I have installed Ghost, which needs nodejs to run. I'm doing this on an Apache Linux server via managed hosting. They kindly let me login with SSH access so I've been able to setup nodejs and Ghost using the standard installation instructions. I installed ghost to the root of my domain so in normal operation someone would go to example.com and it'll show them my blog. Well that's what I'd hoped.
However now when I've come to load Ghost in my browser I discover because I'm accessing it the way I am, and that Ghost doesn't do server configuration, I seem to need an htaccess file to be able to make the site reachable.
So, I have created this htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ http://127.0.0.1:65515/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:65515/$1 [P,L]
</IfModule>
With that saved to the root, if I go to example.com/ghost (the admin panel for Ghost) it works. Nothing wrong there, looks great. If however I try and visit the root, ie goto example.com, instead of showing me the index, it shows me index.js - that is, it literally loads the contents of Ghost's index.js file and displays it instead of parsing it and displaying the main index of the website.
IF however I go to www.example.com then it all works. So whatever the problem is it's because I'm not using www. in the domain.
I would prefer it to work both with or without the www in the URL though. I did try adding some solutions to redirect non-www requests to www.example.com to th ehtaccess but for some reason it still doesn't work (as in if I type example.com it doesn't redirect me to www.example.com).
I think maybe you should be using mod_proxy rather than mod_rewrite. At least, that's what I've used in the past. Apache will catch requests coming in on port 80 and then redirect them to port 65515 where your node server is listening.
http://httpd.apache.org/docs/2.2/mod/mod_proxy.html#proxypass
Like this:
ProxyPass / http://localhost:65515/
I figured it out. Wasn't anything to do with htaccess, or Ghost or nodejs or anything like it. No, instead the problem was the sodding server was caching the website. I discovered the setting to delete the cache and it all started working fine, so, this is now solved.

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

Htaccess in root folder config

I have a website running on the root folder of my host /index.php
I wan't to Block an IP from accessing my website (I know how to do it)
But I wan't to redeirect him to another page to let him know he got banned.
The probleme is that he is banned from every subdomains to so he is not able to view the
banned.html page I made, I need help to fix this.
/index.php (Main website)
/banned/user_name.html (Reason and ban notification for the banned user)
If you place a .htaccess file in a sub-folder, its directives will override the ones that you have in your site main folder... so if you just place another .htaccess file inside of your banned folder that allows him to view that page, all should be well. I'm also willing to bet there are exclusions you could place in your .htaccess file in the root folder, but this may be a simple option for you.
Do you mean something like this?
RewriteCond %{REMOTE_ADDR} ^192\.168\.1\.1$ # Put here IP you want to block
RewriteCond %{REQUEST_URI} !^/banned/user_name.html$
RewriteRule .* /banned/user_name.html [R=301,L]

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.

.htaccess not working on my server

I'm working on my project and now I want to transfer it from my localhost to server. Everything seems to work fine, but .htaccess doesn't work.
The server should be suporting mod_rewrite and print_r(apache_get_modules()); shows mod_rewrite running.
But still, when I type myaddress.com/contact, it shows error 404.
Here is the .htaccess file, but it should be fine, since it runs on my localhost:
RewriteEngine on
RewriteRule ^adminator/?$ adminator/login.php [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?page=$1&detail=$2 [L]
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
Do you have any idea?
Thanks, Mike.
Does the server allow .htaccess? It doesn't really have to.
Try other directives, ask your hosting provider, or check the configuration yourself if possible.
I tried testing your rules they should be fine on urls such as: http://myhost.com/test1 and http://myhost.com/test1/test2 result in requesting url: index.php?page=test1 and index.php?page=test1&detail=test2
So the problem is not your rules, it is your server setup somewhere. As Mewp said check your Apache config file, look for AllowOverride which should be enabled for .htaccess files to overriding default settings.
Ok, fixed it .. deep in the hostings settings, there was an option to activate it .. but I found it after 4 hours of looking around .. :P

Resources