Using .htaccess to get content from directory for sub-domain - apache

I've read a tonne of questions on this and tried a few solutions, but I'm just not getting the results I want.
I'm currently using:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^cp\.example\.net
RewriteRule ^(.*)$ http://example.net/gamepanel/public/$1 [L,NC,QSA]
Which doesn't quite work as intended. When I navigate to cp.example.net I am redirected to http://example.net/gamepanel/public/
What I want to have happen is for cp.example.net to show the content at http://example.net/gamepanel/public, whilst showing the subdomain in the URL.
Also cp.example.net/user should show content from example.net/gamepanel/public/user
Some help on achieving this would be much appreciated!

Turns out I was a little off the mark on what I needed to do and that .htaccess wasn't the best way to do this.
Adding this in my apache files solved it
<VirtualHost *:80>
ServerName cp.example.net
DocumentRoot /var/www/gamepanel/public
<Directory /var/www/gamepanel/public>
Options -Indexes
</Directory>
</VirtualHost>

Related

mod_rewrite entire website to front page ONLY of new website

Now I've written dozens of redirects in my time, some with tricky regex, some more tame, but today, the very simplest redirect is stumping me on a CentOS server, running Apache 2.2.3.
All I'd like to do is redirect every single request on an old domain, regardless of path and query string, to the front page only of a new site. This is why, for example, a mod_alias Redirect directive isn't appropriate, since it appends the path to the new address.
In an Apache conf file, where the virtual server is defined, I now have
<VirtualHost THE.IP.ADDRESS:80>
DocumentRoot "/var/www/html/SITE_ROOT"
ServerName OLD_DOMAIN.com
<Directory "/var/www/html/SITE_ROOT">
Options FollowSymLinks
RewriteEngine On
RewriteRule ^$ https://NEW_DOMAIN [R=301,L]
AllowOverride None
</Directory>
</VirtualHost>
While the redirect to https://NEW_DOMAIN occurs as expected, the path of the original request is always appended, leading to 404 errors on the new site.
For example, visiting http://OLD_DOMAIN.com/asdf
redirects to https://NEW_DOMAIN.com/asdf
...when I'd actually want to arrive at https://NEW_DOMAIN.com/
Why is the path being appended, even though I'm not collecting a pattern match, and am not specifying such a match in the destination?
There are plenty of answers like this on SO already:
Apache redirect to a clean URL
https://stackoverflow.com/a/11590814/1738274
But I can't find a discrepancy comparing these solutions against my own configuration. Any ideas?
RewriteRule ^(.*)$ https://NEW_DOMAIN [R=301,NC,L] should work. I have tested with various URLs and it always redirects to https://NEW_DOMAIN
My config structure looks a bit different:
<VirtualHost *:80>
ServerName OLD_DOMAIN.com
DocumentRoot "C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs"
<IfModule mod_rewrite.c>
RewriteEngine on
Options FollowSymLinks
RewriteRule ^(.*)$ https://NEW_DOMAIN [R=301,NC,L]
.......
.......

How can I get .htaccess to be seen when I make my site live

I have a .htaccess file which (among other things) creates pretty urls. Here's a sample of part of the file:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
...
# Pretty urls
RewriteRule ^home(\d+)*$ ./index.html
RewriteRule ^contact(\d+)*$ ./contact.html
RewriteRule ^enquire(\d+)*$ ./enquire.html
RewriteRule ^terms(\d+)*$ ./terms.html
RewriteRule ^about(\d+)*$ ./about.html
RewriteRule ^owners(\d+)*$ ./owners.html
I am currently moving the site to new hosting on digitalocean. When I test the site out by browsing the IP e.g.
111.222.333.444/html_docs/contact
the .htaccess file is working perfectly. Rewrite rules are on and the site properly rewrites the url and serves (in this case, contact.html) to the browser.
When I change the nameservers and browse the site by domain after propogation e.g.
mydomain.com/contact
the .htacces file fails to do the redirect, at least, it seems like it's not being seen at all. This is weird to me since it works when I'm browsing by ip. I purposely put some rubbish into the .htaccess to test if it was being picked up. Again, if I browse via the ip I get a 500 which I would expect, but when browsing by domain name I do not get an error but the rewrites don't work.
I'm banging my head at this stage and can't figure it out. Any help appreciated,
many thanks,
Wittner
Ok, this was less of a lack of knowledge about how .htaccess works and more to do with not configuring my vhosts settings properly.
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias www.mydomain.com
ServerAdmin me#mydomain.com
DocumentRoot "/var/www/mydomain.com/html_docs/"
<Directory "/var/www/mydomain.com/html_docs/"> <-- Problem was here
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Turns out that in the tag I had left out the path to my domain root. So when I directly browsed the site via the IP the system was able to figure it out, but no while browsing. All my .htaccess stuff is now working. Thanks to all who looked and anyone who replied. Live and learn I guess :-)
cheers,
Wittner

Amazon EC2 - Redirect www.domain.com to http://domain.com

I am new to Amazon EC2 Server, I am facing an issue when I enter www.domain.com my website does not load. When I enter http://domain.com it works fine. I googled and found that this is a canonical issue and I should redirect www.domain.com to http://domain.com. On stack overflow I found this thread and I followed it.
Syntax for 301 redirect of www to non-www using httpd.conf
However my problem did not go away. I added the Virtual Host part as mentioned in my answer to my httpd.conf file.
It now looks like this:
<VirtualHost *:80>
DocumentRoot "/var/www/"
ServerName rafitapp.com
ServerAlias rafitapp.com www.rafitapp.com
<Directory "html/">
allow from all
Options +Indexes
</Directory>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.rafitapp.com [NC]
RewriteRule (.*) http://rafitapp.com%{REQUEST_URI} [R=301,L]
</VirtualHost>
The folder in which my index.html file exists is /var/www/html/.
Please suggest what am I doing wrong. I have googled it for hours and could not fix it.
Any advise would help me a lot.
Thank You
You likely need to set your CNAME record in DNS.
Also, be sure to clear your browser cache when you test.

Different folder as website subfolder

I need some help with URL rewriting. The case is similar with this one.
I have a working Zend Framework site. Now I must add a blog in Wordpress (also working). I've chosen not to indulge in ZF controller-/action-/route-making; I've seen a couple of tutorials about this and I consider them too much for a "plain" redirection. Now, about that "redirection"...
This is how it should look like:
www.site.com (points to /var/www/zf)
www.site.com/blog (points to /var/www/wp)
I know that I should stop www.site.com/blog to enter ZF's innards and I'm currently doing this with RewriteRule ^blog - [NC,L] in its .htaccess, but that's about it. As #jason said, "just pass it through to Wordpress", but I don't know how exactly to do that.
Related question:
I never tried it, but does Apache support this in two different vhosts?
ServerName www.site.com (vhost for ZF site)
ServerName www.site.com/blog (vhost for WP site)
www.site.com (points to /var/www/zf)
www.site.com/blog (points to /var/www/wp)
The easiset way to achive this, where you want a sub-url to point outside the VirtualHost's DocumentRoot directory, is to create an Alias...
Inside the VirtualHost block add:
Alias /blog /var/www/wp
<Directory /var/www/wp>
Options All
AllowOverride All
order allow,deny
allow from all
</Directory>
*This assumes you have PHP enabled in some way for that directory.
The ServerName trick will not work: you can not have path names in a ServerName directive.
For mod_rewrite you should be able to get away with something like this:
RewriteEngine on
RewriteBase /
RewriteRule ^blog/(.*)$ wp/$1 [L]
RewriteRule ^(.*)$ zf/$1 [L]

Maintaining the query string with mod_rewrite

I've correctly configured my website to re-route every piece of traffic from
mywonderfulwebsite.com/folder1/whatever-url.php
to
http://folder1.mywonderfulwebsite.com/whatever-url-as-above.php
Question is: many times, an external website links the page with GET parameters, for example
mywonderfulwebsite.com/folder1/whatever-url.php*?trackingToken=1*
So, question is how to make the mod_rewrite pass in the GET parameters to the "rewrited" url, like this:
folder1.mywonderfulwebsite.com/whatever-url-as-above.php*?trackingToken=1*
Currently, I'm doing the following:
<VirtualHost *>
ServerName mywonderfulwebsite.com
ServerAlias www.mywonderfulwebsite.com
DocumentRoot /var/www/mywonderfulwebsite/
DirectoryIndex index.html
<Directory />
allow from all
Options +FollowSymlinks -Indexes
</Directory>
RewriteEngine On
RewriteRule ^/folder1/(.*)?$ http://folder1.mywonderfulwebsite.com/$1&%{QUERY_STRING} [L,R=301]
</VirtualHost>
This piece of htaccess is awful: for example, trying to access this url:
www.mywonderfulwebsite.com/folder1/atextfile.txt
Redirects to
folder1.mywonderfulwebsite.com/atextfile.txt&
In fact, the mod_rewrite appends the trailing &
How do I fix this issue? How to correctly redirecting to the correct ( also with GET parameters ) url?
Many thanks
Just add the [QSA] flag to your other flags to have the [Q]uery [S]tring [A]ppended automatically.
[L,R=301,QSA]