Apache multiple DocumentRoot - apache

How can I have the following setup in apache?
http://server/ABC/* should be served by /var/www/ABC/*
http://server/PQR/* should be served by /var/www/PQR/*
Every other request should be served by /var/www/Others/index.php (a single file).
Thanks,
JP

Use Alias:
Alias /ABC/ /var/www/ABC/
Alias /PQR/ /var/www/PQR/
Leave the document root pointing to /var/www/Others/index.php. It could do the trick. :)

You can do this with mod_alias, which is part of the apache distribution.
http://httpd.apache.org/docs/current/mod/mod_alias.html
for serving everything else with the single file you would use mod_rewrite. This has many features and depending on your needs you might need to tweak that.. but something like this should work:
RewriteEngine on
RewriteRule ^(.*)$ /index.php?path=$1 [L]
you would put that in a .htaccess file in the document root.

Related

.htaccess folder to virtual-folder rewriting

Is it possible with Apache's RewriteEngine to do the following, and how?
This is the environment:
domain.com has a folder that contains a index.html page, htaccess is set to strip files so domain.com/folder/ opens domain.com/folder/index.html
Now what i need to achieve is display domain.com/folder/ content (the index.html) as it comes from domain.com/virtualfolder/ where virtualfolder does not exist and the url has to show as domain.com/virtualfolder/
I hope what i wrote it is understandable.
Thank you very much
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^virtual-folder/(.*)$ folder/$1 [QSA]
</IfModule>
You need an alias:
Alias /virtualfolder /full_path_to/domain.com/folder
See http://httpd.apache.org/docs/2.2/mod/mod_alias.html for more details.
Alias is the most convenient way, but it is required to put the alias in the httpd.conf. If you rent some web space, the provider will usually only allow you to edit the .htaccess file, and it´s no possible to put an alias in there. In that case, you need to work with mod_rewrite. Just have the same problem right now when typing it.
However, if you rent some web space and you got Plesk 11.5 or 12.0 installed, there´s usually an option "virtual directory" (if not deactivated)

How to have 2 domains on the same website?

For a client we have setup a multilanguage website that consists of an English and Dutch part. The urls look like:
www.example.com for the english website and www.example.com/nl/ for the dutch website
However, I want to change this www.example.comto www.example.nl instead of www.example.com/nl/
How to get this? We already have setup that both domains (example.com and example.nl) point to the same root. But we not yet able to have the /nl/ extension point to example.nl
Hope someone can help me.
Just define two virtual hosts with different DocumentRoot.
Apache Core Features / DocumentRoot Directive
You will need to set the /nl/ directory as root for your .nl Domain.
I've not tried this, but I believe the Move Homedirs to Different Webserver example is a good fit for this problem. First, the example:
On the old webserver we just redirect all /~user/anypath URLs to
http://newserver/~user/anypath.
RewriteEngine on
RewriteRule ^/~(.+) http://newserver/~$1 [R,L]
And my thought on how this would work for you:
RewriteEngine on
RewriteRule ^/nl/(.+) http://newserver.nl/1 [R,L]

How do I rewrite www.sitename.com/thing/thing.php?otherthing=something-like-this to www.sitename.com/something-like-this?

How do I rewrite
www.sitename.com/thing/thing.php?otherthing=something-like-this
to
www.sitename.com/something-like-this?
please help me with this as I can't seem to succeed. My host uses apache 2.2. Many thanks for your help!
Update
No I don't need that trailing ? However, I used the Rewrite rule you offered me and it still ain't working. I also added a RewriteEngine On before the rules.
I have Linux hosting, .htaccess and the code is obviously semantically correct, cause otherwise I would get the all so popular 500 internal server error. I placed the .htaccess file in the folder thing and in the root of the site, but it still won't work.
There should be an option to display it in directory format instead of the PHP ? format. If not, you could use the .htaccess mod_rewrite rule to make that display in the /folder/ way.
The way I do it is that I just upload my files and each page name is index.html and then I create folders, and put each index.html in the folder. Like this:
/guidelines/
In that folder is index.html, so instead of it being /guidelines.html it's /guidelines/
Looks better without .html
You need to use mod_rewrite:
RewriteCond %{QUERY_STRING} ^otherthing=(.*)$
RewriteRule ^thing/thing.php$ /%1? [L]
No idea if you meant to have that trailing ? at the end of the rewrite, I don't think that's possible. Note that the ? at the end of the RewriteRule is to get rid of the query string, otherwise, the rewritten URL will still have the ?otherthing=something-like-this at the end.

rewritemap for SEO and pretty URLs

I am attempting to redirect & rewrite some dynamic PHP URL's to pretty and SEO friendly URLs. I have manged to do this successfully through .htaccess with the following code:
RewriteCond %{QUERY_STRING} ^somevar=green&nodescription=([a-zA-Z0-9_-]*)$
RewriteRule (.*) /green\/%1\/? [L,R=301]
RewriteRule ^green/([^/]*)/$ /script.php?somevar=green&nodescription=$1&rewrite=on [L]
This creates a somewhat pretty URL as follows:
http://www.mysite.com/green/aA43-/
As I say, this works absolutley fine. Apart from one thing. The parameter nodescription contains a non-descriptive random set of letters, numbers and other characters.
I would like to rewrite the nodescription parameter to a more descriptive one. I understand that I can do this with a rewritemap through Apache. However, I have no experience at doing soemthing like this, and I'm not entirely sure where to start.
Normally I would simply alter script.php so that it contains more descriptive parameters, but this time I have no control over the script; I am pulling it from another site using cURL.
Can anybody give me an example of how to pull this off?
Thanks!
Matt
Well, to answer my own question, to pull this off you need access httpd.conf file on your apache server. My shared hosting company didn't allow access to this file (I doubt any would allow you access).
So I bit the bullet and purchased a VPS. I will post the steps I took here in order to set the rewritemap up in the hope that it will help a lost soul :) Ok, here goes...
My VPS has WHM installed, so in WHM I went to:
Server Configuration >> Apache Configuration >> Include Editor
Pre Virtual Host Include >> All Versions
This feature takes any text you put in and includes it in your httpd.conf file without worrying that it will be overwritten at a later stage. If you don't have WHM on your server then you can add the text directly to your httpd.conf file; make sure it is outside and before any virtual hosts.
OK, so I included the following map declaration and rewrite rule:
#Map to redirect (swaps key and value)
RewriteMap rwmap txt:/home/*/public_html/rdmap.txt
<Directory /home/*/public_html/test>
Options All -Indexes
RewriteEngine on
RewriteRule ^url/([^/]*)/$ /script.php?foo=${rwmap:$1|$1}&rewrite=on [L]
</Directory>
The actual map is a simple text file containing key/value pairs - you need to place this file in the directory declared in RewriteMap rwmap txt:/home/*/public_html/rdmap.txt.
And there you go. Apache now rewrites my URLs for me and I now have some nice and pretty SEO optimized links thanks to my rewrite map! Hoorah!
RewriteEngine on
RewriteRule ^green/([^/]*)/(.*)$ /script.php?somevar=green&nodescription=$1&rewrite=on [L]
This rewrite will allow you to pass "arbitrary text" that has nothing to do with the rewrite. For example:
http://www.mysite.com/green/aA43-/some-seo-boosting-title
Will still reroute correctly to script.php; the latter part will simply be ignored by the rewrite.

How to configure apache (ubuntu) such that www.mysite.com will direct to www.mysite.com/drupal6/?

I am a newbie to ubuntu and apache. Can someone tell me how I could direct to
www.mysite.com/drupal6
when user address www.mysite.com?
Thanks a lot.
Cheers.
If you are running Apache and Ubuntu, there is actually a really easy way to force this redirect using a simple php script.
Create an index.php file in the root of your server and paste the following code into it
<?php header("location: drupal6/") ?>
This will cause the site to auto-redirect to the drupal6 folder whenever it is visited.
This should work. Create a file in the root folder of your server called .htaccess - the dot at the beginning is very important as this helps the server identify the file as a hidden / system config file.
Open the file and paste the following lines of code in :
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ www.mysite.com/drupal6/$1 [R,L]
This should force all traffic to the server to redirect to your custom folder.
A brief explanation of the .htaccess code
If you want rewrites to work, you have to enable the Rewrite Engine and tell the server to follow symlinks.
The second section establishes the rule - specifically applying it to all traffic on the standard web port of 80.
The final line tells the server to grab everything after the URL and append it to the new address (mysite.com/drupal6).
There's a lot more you can do with .htaccess files but you really need to Google for good examples to test out.
Look at Apache's mod_rewrite documentation. You will need a RewriteRule in your apache configuration at the minimum, you may also need RewriteCond's to define when the RewriteRule is used.
Your rewrite pattern will be rewriting the REQUEST_URI with something from: ^/$ to: /drupal6. The ^ and $ are essential to prevent Apache getting into an infinite loop while rewriting the base URI by only matching "/" and not "/anything-else".
I assume you're on a recent version of Ubuntu and Apache? If so, see the Apache 2.2 documentation on mod_rewrite.