Apache alias from sub domain to domains article - apache

I wonder is it possible to make an alias from sub domain, let's say http://sub.domain.com, to http://domain.com/some-article. When user types in address bar sub.domain.com, he should see that article(address in address bar should be the same sub domain, so I think redirect its not what I need). Any ideas?
Your help would be appreciated.

The Apache ServerAlias only covers a hostname and not a fully qualified URI.
In the above example you should use mod_rewrite, making sure to omit the R flag since you don't want to redirect i.e provide a 30X status code in the response.
So in the above example you could have the following
<VirtualHost *:80>
...
ServerName domain.com
ServerAlias sub.domain.com
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteCond %{REQUEST_URI} ^$
RewriteRule ^$ /some-article [L]
RewriteCond %{HTTP_HOST} ^sub.domain.com$
RewriteRule ^(.*) http:/domain.com/some-article$1 [L]
...
</VirtualHost>

Related

Force to SSL maintaining the URL

I don't have much experience with VHOST and SSL, I tried lot of possibilities but none of them is working.
I have a website with 2 folders:
www.example.com/userpage
www.example.com/adminpage
If I go to www.example.com, my loginsystem automatically redirect to www.example.com/userpage.
If I want to go to the admin section, I have to write manually www.example.com/adminpage.
Now I switched to SSL, and everything is working if I type https://....
But I cannot understand how to force the redirect from http to https.
I wrote this in my apache vhost file:
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs"
ServerName example.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com
RewriteCond ${HTTPS} !=on
RewriteRule (.*) https://example.com/$1 [R=301,L]
</VirtualHost>
But it's not working.
How can I manage it?
If I write www.example.com/adminpage it have to redirect me to https://www.example.com/adminpage.
Actually it should work for every subfolder, if for example I send an email to a user saying "hey user, please check your account www.example.com/user/check_account.php?userid=14125114
it have to then redirect it to automatically:
https://www.example.com/user/check_account.php?userid=14125114
so it should work for every page and every subfolder.
Thank you for your suggestions
Should be quite simple. Replace this block:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com
RewriteCond ${HTTPS} !=on
RewriteRule (.*) https://example.com/$1 [R=301,L]
With this:
Redirect permanent "/" "https://example.com/"

dynamic vhost configuration based on a substring of the URI

I'm looking to perform a simple redirection to a root directory based on a substring of the URI. For example:
app1_{substring1}.mydomain.com will redirect to C:/wamp/www/substring1
app1_{substring2}.mydomain.com will redirect to C:/wamp/www/substring2
ServerAlias allows only wildcard...
Any ideas to implement that ?
Apache version: 2.2.22 (WAMP version)
Try this:
ServerAlias *.mydomain.com
DocumentRoot C:/wamp/www
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{HTTP_HOST} ^app1_([a-z0-9A-Z]+)\.mydomain\.com [NC]
RewriteRule ^ /%1%{REQUEST_URI} [QSA,L]

Apache: Don't apply RewriteRule if requested host is a IP address

How doesn't Apache apply an RewriteRule if the requests isn't the normal domain name but instead the server IP?
Current rules (to make domain just accessible without the www):
RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
I tried already to add RewriteCond %{HTTP_HOST} !^123.321.123.321$ [OR] but it seemed not to work.
If you have access to the server config file, this works for me (setting it in httpd.conf):
<Location />
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^123.123.123.123$
RewriteRule (.*) http://www.mainsite.example.com/ [L]
</Location>
If I visit the site by IP address, it isn't rewritten. If I use (eg.) http://mainsite.example.com, the address is rewritten (to include the www.). The [L] (when used in httpd.conf) stops rewrite processing there (instead of now trying to rewrite the new address).
In there you can also have
ServerName www.mainsite.example.com
ServerAlias mainsite.example.com
ServerAlias www.mainsite
ServerAlias mainsite
ServerAlias 123.123.123.123
so that people can use any of those addresses (the 2nd and 3rd aliases let internal users omit the domain), instead of rewriting to force or add www.

conditional DirectoryIndex based on IP address, using .htaccess

I've got this in httpd.conf:
<VirtualHost xx.xxx.xx.xxx>
Options All +ExecCGI
ServerAdmin hostmaster#thehost.com
DocumentRoot /var/www/html/domain.com
ServerName dl.domain.org
DirectoryIndex dlindex1.html
</VirtualHost>
... which is fine (what I need as the DirectoryIndex for our 'dl.domain.org' subdomain), but now I also need to alter that DirectoryIndex based on IP address, using .htaccess.
Is this possible?
Other SO posts are telling me that I cannot set DirectoryIndex conditionally.. but instead have to use a RewriteRule.
If that is true, OK, but what RewriteCond and RewriteRule?
I am pretty noob in Apache, but anyway have tried many things, including (where the actual IPs are those of our 2 devs):
RewriteCond %{REMOTE_ADDR} ^111\.222\.333\.444$ [OR]
RewriteCond %{REMOTE_ADDR} ^555\.666\.777\.888$
RewriteCond %{SERVER_NAME} ^dl.domain.org
RewriteRule ^(.*)/$ $1/dlindex2.html
..or even just (as an absolute test):
RewriteCond %{REMOTE_ADDR} ^555\.666\.777\.888$
RewriteRule (.*)/dlindex1.html$ $1/dlindex2.html
But it seems that whatever I try it just serves up the DirectoryIndex dlindex1.html as per httpd.conf, as opposed to the dlindex2.html I want served up as the default page in that subdomain when a devs IP is calling.
Can any one point me to what I can do to get what I am after? i.e. this: ...to actually, or even just effectively, alter DirectoryIndex based on IP address, using .htaccess, on the fly?
2 ways turned out to work for me:
https://unix.stackexchange.com/questions/44129/conditional-directoryindex-based-on-ip-address-using-htaccess
https://webmasters.stackexchange.com/questions/32727/conditional-directoryindex-based-on-ip-address-using-htaccess
e.g.:
RewriteEngine On
RewriteCond %{REMOTE_ADDR} ^12\.34\.56\.78$
RewriteCond %{REQUEST_URI} index\.html$
RewriteRule .* /index1.html
RewriteCond %{REMOTE_ADDR} !^12\.34\.56\.78$
RewriteCond %{REQUEST_URI} index\.html$
RewriteRule .* /index2.html

A simple mod_rewrite rule in my .htaccess to redirect a friendly URL to the script

hi i'd like to rewrite http://www.domain.com/site_reg/index.php to http://www.domain.com/signup how would I do this. both for www.domain.com and domain.com?
EDIT: maybe i have it backwards... user types in http://www.domain.com/signup and http://www.domain.com/site_reg/index.php load the page
This will work for both www.domain.com and domain.com, assuming you have ServerName and ServerAlias set in your VirtualHost
Try:
<VirtualHost>
ServerName domain.com
ServerAlias www.domain.com
RewriteEngine On
RewriteRule ^/site_reg/(.*) /signup/$1 [R=301,L]
</VirtualHost>
You want to assign a 301 Redirect to tell search engines, browsers, etc. that the page has permanently moved. If the move is not permanent, use the temporary status code: 307
Try:
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} !FeedBurner
RewriteRule ^site_reg\/index\.php$ http://www.domain.com/signup [R,L]
Put this in your base .htaccess:
RewriteEngine on
RewriteRule ^site_reg/index\.php$ http://www.domain.com/signup [R,L]