Rewriting subdomain to subfolder with htaccess - apache

I'm attempting to use .htaccess in the root folder of an Ubuntu/Apache2 server in order to mask a subdomain to subfolder and I keep getting a 500 Internal Error. I know that I'm doing something stupidly wrong and it is some silly error causing the problem. I've checked all of the similar threads on SO and online and whenever I try their advice the 500 continues.
Here's my code.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^admin\.mydomain\.com.*$
RewriteRule (.*) intranet/$1 [L]
What I want to occur is that if a user visits admin.mydomain.com they will get the contents of the folder admin.mydomain.com/intranet/ but their URL bar will still be admin.mydomain.com. Any idea what I'm doing wrong?
In addition, some of the threads online talked about possible problems with this system. Is this the best way of doing this masking, should I be using a vhost setup?

The rewrite rule should work, although I'd probably write
RewriteEngine on
RewriteBase / #omit if in a <Directory> or .htaccess
RewriteCond %{HTTP_HOST} =admin.mydomain.com
RewriteRule ^(?!intranet/).* intranet/$0
Now, you might want to check you Apache error log. It will probably tell you what the error is. My guess is that you did not enable mod_rewrite in httpd.conf.

Related

How to fix this error - Automatically redirect subdomain to folder path

HOW CAN I FIX THIS ERROR ?
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI}::%1 !^/([^/]+).*?::\1
RewriteRule ^(.*)$ /%1/$1 [L]
error > image
When i open this link : abc.example.com
Automatically read folders
/abc/sub/
How can i change it to
/sub/abc/
You may want to check out Apache Virtualhosts.
From the documentation:
mod_rewrite is not the best way to configure virtual hosts. You should first consider the alternatives before resorting to mod_rewrite. See also the "how to avoid mod_rewrite document.
For information on using Apache Virtualhosts to mass-remap hosts to folders, see this guide:
https://httpd.apache.org/docs/2.4/vhosts/mass.html
(Specifically, see the section on "Simplified Dynamic Virtual Hosts")
I acknowledge that I'm not explicitly answering the question that you're asking, but I am trying to point you to an easier (and more supported) way of accomplishing the same thing.

.htaccess URL Rewrite not working

I've never been good at .htaccess, I'm trying to copy and paste some code that worked on another one of my domains and modify it to work here. I will have several rewritten URLs, some static, some dynamic, but I can't even get the simplest of them to work. This one is testable here: http://lindseymotors.com/home
Clearly, index.php is available because if you access http://lindseymotors.com it works.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.* [NC]
RewriteRule ^home$ index.php
RewriteRule ^home/$ index.php
# When answering, if you could write a statement that would combine
# both of the statements above into one that would be appreciated.
As I said, these same conditions worked on another one my domains because I copied the code right over. I asked my server admin to double check everything on his end and it was fine. Any ideas?
Only thing I can think of is make sure the use of .htaccess is really on. The easiest way you can check since your server admin says it's fine is to put random text at the top of your .htaccess file. If your .htaccess file is being read and .htaccess files are enabled, it should throw a 500 internal server error. If not, then they don't have .htaccess files enabled and need to add AllowOverride All to the Apache config vhost.
Here is your rule combined into one as you noted. You really don't need the RewriteCond, but I will leave since you were using it previously.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.* [NC]
RewriteRule ^home/?$ index.php [L]

How to make Apache rewrite blog URLs?

I've installed WAMP, made an alias, and put into that folder a ".htaccess" file. My goal is to have a URL such as "foo.com/blog/bar-baz" internally call "display.php?name=bar-baz". Among various things I tried the following:
RewriteRule ^blog/(.+)$ display.php?$1
However, this gives a "The requested URL /Users/.../public/display.php was not found on this server". Playing around I was able to get this to work:
RewriteRule ^blog.php$ /personal/display.php
Advice greatly appreciated.
You can try this rule from root .htaccess:
RewriteEngine On
RewriteRule ^blog/(.+)$ /personal/display.php?name=$1 [L,QSA,NC]

Changes to RewriteRule in .htaccess not taking effect

I had this rewrite rule set up in .htaccess and it was all working fine...
Options +FollowSymLinks +ExecCGI
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/mypage(.*)$ [NC]
RewriteRule ^(.*) http://example.com/PHProxy/poxy-0.5b2/index.php?url=http://example.org/mypage [L,R=302,NC]
However, when I change the url in the RewriteRule to
http://example.com/PHProxy/poxy-0.5b2/index.php?url=http://example.org/mypage it still redirects to the old URL.
After some research, I added a syntax error into the .htaccess file to check the .htaccess file was being used (and indeed it was - as it resulted in an Internal Server Error when you tried to load a page from that directory).
There seems to be some caching somewhere, but I'm not sure. Any ideas why my change is not being picked up / how to troubleshoot and resolve?
Problem solved. Just noticed that there is a mypage subdirectory which still contained the old rewrite rule, so that was the one being executed.

Rewriting a redirected URL with mod_rewrite

Here is my setup :
I have a website located at www.cabsh.org/drupal
I want to use mod_rewrite to do 2 things :
Redirect www.cabsh.org to http://www.cabsh.org/drupal/index.php (I got this one)
Rewrite /www.cabsh.org/drupal/index.php to www.cabsh.org/site/index.php
I cannot figure how to achieve the 2nd point. I'm using .htaccess files since I cannot use the main server configuration. Can anyone help me getting this to work?
Thanks!
From what I get from your comment, you just want something like this:
RewriteEngine on
# Prevent a request directly to the /drupal folder
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/drupal/
RewriteRule ^drupal/(.*)$ /site/$1 [R=301,L]
# Change a request for /site/(anything) to /drupal/(anything)
RewriteRule ^site/(.*)$ /drupal/$1
Be careful though, since Drupal (being in the Drupal folder) might generate links that point to /drupal instead of /site, which is seemingly not what you want.