.htaccess folder to virtual-folder rewriting - apache

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)

Related

How can I have redirect or make an alias for my websites directories?

Real directory structure:
http://example.com/directory1/directory2/directory3/hiddendirectory/directory4/
I need the file to open as:
http://example.com/hiddendirectory/directory4/
in the browser, but in reality the directory still be in the first location.
I am on shared hosting with hostgator and do not have full privileges so I cannot run any scripts.
I've done some research and I've messed around trying create an alias, but I can't get anything to work.
Here is what I put in the .htaccess file:
Alias /hiddendirectory/directory4 /directory1/directory2/directory3/hiddendirectory/directory4
But I always get a 500 server error. Any advice would be appreciated.
Alias directive runs on the Server config context not in htaccess.
In htaccess you can use mod_rewrite to rewrite your dirs.
RewriteEngine On
RewriteRule ^hiddendir/dir4/?$ /dir1/dir2/dir3/hiddendir/dir4 [NC,L]
This will rewrite
/hiddendir/dir4/
to
/dir1/dir2/dir3/hiddendir/dir4

Changing a file's URL without physically moving it

I have a site, running Linux + Apache.
I have a file in my root directory, let's say file.php.
I want the URL to the file to be "domain.com/newdir/file.php", but I don't want to actually create the newdir and move the file there because it would be a huge hassle to update many many links all over my site.
Is there a way to accomplish this, meaning making the file accessible by the new URL without moving it?
Thank you.
On this site: workwith.me, you can find information about .htaccess and mod_rewrite. For your example you have to make a file called .htaccess and put it in the root directory. The file should contain these directives:
RewriteEngine on
RewriteRule ^newdir/file.php$ /file.php [L]
You can do this for every file you want to rename.
Four possible solutions I can think of:
If your OS supports it, create a symlink:
mkdir /home/foo/htdocs/newdir
ln -s /home/foo/htdocs/file.php home/foo/htdocs/newdir/file.php
... and make sure Apache is configured to follow them:
Options FollowSymLinks
Create an Alias or AliasMatch (probably overkill)
Good old mod_rewrite:
RewriteEngine One
RewriteRule ^newdir/file\.php$ file.php [L]
Ugly: use a custom 404 error page with a PHP script that checks $_SERVER['REQUEST_URI'].
I guess the standard solutions are #1 and #3.

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.

Apache multiple DocumentRoot

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.

htaccess rewrite question

I want to rewrite the url http://mydomain/myapp/fakefolder to http://mydomain/myapp/index.php
I tried the following rule but thats not working
RewriteEngine On
RewriteRule ^fakefolder$ index.php
The .htaccess file is located inside myapp.
Kindly help
Thanks
vineet
To begin with, your folder is not called vineetstore: it's called fakefolder.
The corrected rule works for me so I'd dare say your Apache installation is not configured to read .htaccess files in such location. You can easily test that: make a syntax error on purpose and see whether your site crashes.
Find your virtual host or site definition and make sure you have this directive:
AllowOverride All