Apache Redirect Rule Needed.Thanks! - apache

everyone
I have one problem here.
My site is powered by Mediawiki,
which means, the visitors are supposed to visit links like this:
hxxp://www.example.com/wiki/SOMETHING
I want to redirect the user to the Main Page if he is trying to access something else:
hxxp://www.example.com/NOTwiki/SOMETHING -> hxxp://www.example.com/wiki/Main_Page
and
hxxp://www.example.com/NOTwiki -> hxxp://www.example.com/wiki/Main_Page
And "/wiki" is case sensitive,
which means:
hxxp://www.example.com/WiKi/SOMETHING -> hxxp://www.example.com/wiki/Main_Page
I am using a virtual host like this:
<VirtualHost 12.34.56.78:80>
ServerName example.com
ServerAdmin admin#example.com
ServerAlias www.example.com
DocumentRoot /srv/www/example/public_html/mediawiki/
ErrorLog logs/AP/error_log
</VirtualHost>
I am new on Apache and still learning.
Be specific,Please.
Thanks a lot!

This config should be:
<IfModule mod_rewrite.c>
RewriteBase /
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteCond %{REQUEST_URI} ^index.php
RewriteRule .* - [L]
RewriteRule !^wiki(/.*)?$ wiki/Main_Page [R=301,L]
</IfModule>
The RewriteCond/RewriteRule says "If the start of the path is not /wiki, rewrite all URLS to /wiki/Main_Page using a 301 redirect (R=301), and do not process any more rules (L)"

Related

Using mod_rewrite to redirect home page ONLY on HTTPS

I followed this example "using-mod-rewrite-to-redirect-home-page-only" but I'm not sure it functions on a https/SSL
The site home page is https://www.somasimple.com and I want it to be redirected to https://www.somasimple.com/forums/home
I tried without success this at the end of the htaccess file =>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^somasimple\.com$
RewriteRule (.*) https://www.somasimple.com/$1 [R=301,L]
RewriteRule ^$ forums/home [L]
To redirect from https://www.somasimple.com to https://www.somasimple.com/forums/home just add a simple redirect match in virtualhost, no need for mod_rewrite or any other convoluted messes you may find out there.
<Virtualhost *:443>
ServerName www.somasimple.com
DocumentRoot /path/to/docroot
SSLEngine on
#.... (other SSL directives)
# Redirect / to /forums/home
RedirectMatch ^/$ /forums/home
</Virtualhost>

rewrite subdomain to direcory in same server

I have a domain, lets say mydomain.com.
I want every subdomain of this to load the contents of the directory with the same name... For example, if someone writes
http://sub.mydomain.com/index.php
I want to show him the contents of
http://sub.mydomain.com/sub/index.php
Still, I want it to show in the addressbar the http://sub.mydomain.com/index.php
The apache vhost is like
<VirtualHost *:80>
ServerName *.mydomain.com
DocumentRoot /var/www/vhosts/mydomain.com/subdomains/subs/www
... more stuff ...
</VirtualHost>
so in the filesystem, the files for the example above would be under the directory
/var/www/vhosts/mydomain.com/subdomains/subs/www/sub
I tried many of the proposed solutions here, but most of them were redirects to some other domain/subdomain or end up in a redirect loop :(
tia
You need to add some rewrite rules that will examine the sub domain and then rewrite to the relevant path:
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias *.mydomain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} (.*)\.mydomain\.com
RewriteRule ^/(.*)$ /%1/$1
DocumentRoot /var/www/vhosts/mydomain.com/subdomains/subs/www
... more stuff ...
</VirtualHost>
What this will do is capture anything preceding ".mydomain.com" then rewrite it into the URL as %1, $1 will be the requested resource such as index.html
Be aware this might trip you up if www.mydomain.com is a valid domain for your site!
Try this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.[^.]+\.[^.]+$ [NC]
RewriteRule !^sub(|/$) /sub%{REQUEST_URI} [L,NC]

Dynamicly set document root for subdomains using regex

I've setup subdomain wildcard to accept anything subdomian,the main problem is that i use DirectAdmin and i am not able to point subdomain to specific path using DA API
i want to point any subdomian to a folder named as subdomian which is exist in subdirectory
for example sub.domian.com should point to /public_html/subdomains/sub
something like this
<VirtualHost *:80>
VirtualDocumentRoot /public_html/subdomains/%1
</VirtualHost>
this would be my ideal if i set subdomains which content a word like ".x" to point in that path
maybe like
<VirtualHost *.x:80>
VirtualDocumentRoot /public_html/subdomains/%1
</VirtualHost>
sorry if question sounds stupid
looks like this is what you need:
<VirtualHost *:8080>
ServerName localhost
ServerAlias *.localhost
#the %1 is the asterisk value! No regexp supported!
VirtualDocumentRoot /home/%1/project
VirtualScriptAlias /home/%1/project
<Directory /home/*/project/>
#...
</Directory>
</VirtualHost>
This can be done totally using htaccess .
You have to enable wildcard subdomain name *.domain.com and point it to domain.com
Then you can use the following types of rules :
RewriteCond %{HTTP_HOST} !www.domain.com [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-]+).domain.com [NC]
RewriteRule ^(.*/) /%1/index.php [PT,L]
You can modify RewriteRule as files for subdomains are stored .
found the answer
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !www\.domain\.com$[NC]
RewriteCond %{HTTP_HOST} ^(.*?)\.domain\.com$
RewriteCond %{REQUEST_URI} !folder/(.*?)/
RewriteRule ^(.*) /folder/%1%{REQUEST_URI} [L]
</IfModule>

virtual hosts map to directory structure

I have read the mass virtual host help on apache but as a relative newbie I am left a little confused. I know my problem must be acheiveable as it is a pretty basic problem but i am lost...
Basically I have lots of virtual hosts pointing to my server and the file structure that they point to for their DocumentRoot is consistent... e.g..
www.mydomain.com -> /home/blah/vhosts/mydomain.com/www/public
abc.mydomain.com -> /home/blah/vhosts/mydomain.com/abc/public
www.another.co.uk -> /home/blah/vhosts/another.co.uk/www/public
def.another.co.uk -> /home/blah/vhosts/another.co.uk/def/public
If possible, I also need to redirect non-www.* to www.* but taking into account the possibility of a subdomain, so that:
mydomain.com is redirected to www.mydomain.com
abc.mydomain.com is NOT redirected to www.mydomain.com as it is
handled with the definition above (due to directory structure)
Is this at all possible?
Here what I do for all my vhosts: I'm using Apache writemap.
Create a new "partner.txt" file with partners like this:
0 www
1 partner1
2 partner2
3 partner1
Then compile it, and add use it into your rewriterules to find out whether the prefix is a partner or not, like this:
<VirtualHost *>
ServerAdmin webmaster#mydomain.fr
DocumentRoot "/web/htdocs/olivier/mydomain.fr/dev/website"
ServerName mydomain.fr
ServerAlias *.mydomain.fr
ErrorLog "/web/logs/mydomain.error.log"
CustomLog "|/opt/httpd/bin/rotatelogs /web/logs/mydomain.fr/access_log.%Y-%m-%d-%H_%M_%S.log 5M" combined
ErrorDocument 404 /404.php
RewriteEngine On
# trying to hack = redirect:
RewriteRule (.*)setup.php http://disneyland.fr/ [NC,R,L]
RewriteRule (.*)admin(.*) http://disneyland.fr/ [NC,R,L]
# if your host doesn't begin with "www" add it and redirect:
RewriteCond %{HTTP_HOST} ^mydomain\.(fr|com|net|org|eu) [NC]
RewriteRule (.*) http://www.mydomain.%1$1 [QSA,R=301,L]
RewriteMap partners \
dbm:/web/htdocs/olivier/mydomain.fr/rewriterules/partners.map
# test if known partner:
RewriteCond %{HTTP_HOST} (([a-zA-Z0-9\-]+)\.)mydomain.com$
RewriteRule (.*) - [QSA,E=PARTNER:${templates:%1|notfound}]
# if partner not found or empty, 404:
RewriteCond %{ENV:PARTNER} ^$ [OR]
RewriteCond %{ENV:PARTNER} notfound
RewriteRule .* - [R=404,L]
</VirtualHost>

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]