Mod Rewrite is giving me a hard time - apache

I'm currently using wamp on windows 7. I'd like to clean my urls obviously. I've tried to find the current syntax and what not, but I haven't figured it out.
My path right now is localhost/rs/index.php
When I go to localhost/rs/user it gives me a 404, but localhost/rs/ gives me the index.php page.
This is what I have in my .htaccess file at the www directory of wamp.
RewriteEngine On
RewriteRule ^/$ index.php
RewriteRule ^/([a-z]+)$ index.php?page=$1
RewriteRule ^/([a-z]+)/$ index.php?page=$1
I have un-commented the line
LoadModule rewrite_module modules/mod_rewrite.so
in the httpd.conf file of Apache
What's wrong? Is my .htaccess file in the wrong spot? Is my syntax wrong?
Thanks!

Place your .htaccess in /rs folder and try
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9]*)/?$ index.php?page=$1 [L]
Hope this will help

Your .htaccess file should be placed in '/rs' folder, in the same directory where index.php is.
I've tried with enabled rewrite log, and what I saw there, when tried to access localhost/test/user:
::1 - - [12/Oct/2012:09:53:11 +0400] [localhost/sid#68a898][rid#1cc4d48/initial] (3) [perdir D:/Development/htdocs/test/] strip per-dir prefix: D:/Development/htdocs/test/user -> user
::1 - - [12/Oct/2012:09:53:11 +0400] [localhost/sid#68a898][rid#1cc4d48/initial] (3) [perdir D:/Development/htdocs/test/] applying pattern '^/$' to uri 'user'
::1 - - [12/Oct/2012:09:53:11 +0400] [localhost/sid#68a898][rid#1cc4d48/initial] (3) [perdir D:/Development/htdocs/test/] strip per-dir prefix: D:/Development/htdocs/test/user -> user
::1 - - [12/Oct/2012:09:53:11 +0400] [localhost/sid#68a898][rid#1cc4d48/initial] (3) [perdir D:/Development/htdocs/test/] applying pattern '^/([a-z]+)$' to uri 'user'
::1 - - [12/Oct/2012:09:53:11 +0400] [localhost/sid#68a898][rid#1cc4d48/initial] (3) [perdir D:/Development/htdocs/test/] strip per-dir prefix: D:/Development/htdocs/test/user -> user
::1 - - [12/Oct/2012:09:53:11 +0400] [localhost/sid#68a898][rid#1cc4d48/initial] (3) [perdir D:/Development/htdocs/test/] applying pattern '^/([a-z]+)/$' to uri 'user'
::1 - - [12/Oct/2012:09:53:11 +0400] [localhost/sid#68a898][rid#1cc4d48/initial] (1) [perdir D:/Development/htdocs/test/] pass through D:/Development/htdocs/test/user
From the first line it is clear, that mod_rewrite is stripping beginning '/', and you are getting 'user' instead of '/user'. So, rewrite rules should be written without '/', that is:
RewriteEngine On
RewriteRule ^$ index.php
RewriteRule ^([a-z]+)/?$ index.php?page=$1
Also notice, that I've combined two last rules by writing '/?'. That means '/' symbol at the end of url is optional.
In order to turn on rewrite log, set the following in your httd.conf file:
#
# Logging for mod_rewrite
# Use RewriteLogLevel 3 only for debug purposes
# Normally use RewriteLogLevel 0
#
<IfModule rewrite_module>
RewriteLogLevel 3
RewriteLog "logs/rewrite.log"
</IfModule>
That way log will be created in logs/rewrite.log file. And this is usually the best way to examine what goes wrong.

If you're running it in subfolder you need to add this line after RewriteEngine On
RewriteBase /rs
Also, make sure that in your apache virtual host section has this value
AllowOverride All

Related

htaccess canonical URL subdirectory

I have the same problem as htaccess - canonical URL when redirecting to subdirectory, but the solution there appears to use a hardcoded host name in the htaccess file which I can't do.
The following is in my htaccess file in the root directory which works fine for redirecting all requests into the /public directory with the exception of node_modules:
<IfModule mod_rewrite.c>
RewriteEngine on
# Allow node_modules
RewriteRule ^node_modules($|/) - [L]
# Rewrite everything to public
RewriteRule ^(.*)$ public/$1 [L,QSA]
</IfModule>
However I realize that the pages can be accessed through two different URLs, for example:
https://localhost/application1/foo/books.php
https://localhost/application1/public/foo/books.php
How do I either prevent the second one (ideally) or have it redirect to the first one?
I tried various RewriteCond statements with %{THE_REQUEST} but they turned out to be infinite loops. As mentioned above this needs to be hostname-agnostic as the application runs on different environments.
Update
I tried #SuperDuperApps answer below with the following in my .htaccess, which seemed to make no difference:
RewriteEngine on
RewriteCond $1 !^node_modules($|/)
RewriteCond $1 !^public($|/)
RewriteRule ^(.*)$ public/$1 [L,QSA]
RewriteRule ^public/ - [L,R=404]
After enabling RewriteLogLevel 3 in my dev server, this is what appears when I access a file with /public in the URL:
192.168.33.1 - - [27/Jan/2017:22:52:45 --0500] [localhost/sid#7f4a0d1d2cf0][rid#7f4a0d6a5d58/initial] (1) [perdir /var/www/html/application1/public/] pass through /var/www/html/application1/public/common/assets/js/nav.min.js
And this is when I access the same file without /public in the URL (desired behaviour):
192.168.33.1 - - [27/Jan/2017:22:48:45 --0500] [localhost/sid#7f4a0d1d2cf0][rid#7f4a0d684738/initial] (3) [perdir /var/www/html/application1/] add path info postfix: /var/www/html/application1/common -> /var/www/html/application1/common/assets/js/nav.min.js
192.168.33.1 - - [27/Jan/2017:22:48:45 --0500] [localhost/sid#7f4a0d1d2cf0][rid#7f4a0d684738/initial] (3) [perdir /var/www/html/application1/] strip per-dir prefix: /var/www/html/application1/common/assets/js/nav.min.js -> common/assets/js/nav.min.js
192.168.33.1 - - [27/Jan/2017:22:48:45 --0500] [localhost/sid#7f4a0d1d2cf0][rid#7f4a0d684738/initial] (3) [perdir /var/www/html/application1/] applying pattern '^(.*)$' to uri 'common/assets/js/nav.min.js'
192.168.33.1 - - [27/Jan/2017:22:48:45 --0500] [localhost/sid#7f4a0d1d2cf0][rid#7f4a0d684738/initial] (2) [perdir /var/www/html/application1/] rewrite 'common/assets/js/nav.min.js' -> 'public/common/assets/js/nav.min.js'
192.168.33.1 - - [27/Jan/2017:22:48:45 --0500] [localhost/sid#7f4a0d1d2cf0][rid#7f4a0d684738/initial] (3) [perdir /var/www/html/application1/] add per-dir prefix: public/common/assets/js/nav.min.js -> /var/www/html/application1/public/common/assets/js/nav.min.js
192.168.33.1 - - [27/Jan/2017:22:48:45 --0500] [localhost/sid#7f4a0d1d2cf0][rid#7f4a0d684738/initial] (2) [perdir /var/www/html/application1/] strip document_root prefix: /var/www/html/application1/public/common/assets/js/nav.min.js -> /application1/public/common/assets/js/nav.min.js
192.168.33.1 - - [27/Jan/2017:22:48:45 --0500] [localhost/sid#7f4a0d1d2cf0][rid#7f4a0d684738/initial] (1) [perdir /var/www/html/application1/] internal redirect with /application1/public/common/assets/js/nav.min.js [INTERNAL REDIRECT]
192.168.33.1 - - [27/Jan/2017:22:48:45 --0500] [localhost/sid#7f4a0d1d2cf0][rid#7f4a0d676688/initial/redir#1] (1) [perdir /var/www/html/application1/public/] pass through /var/www/html/application1/public/common/assets/js/nav.min.js
This should do it:
RewriteEngine on
# Allow node_modules
RewriteCond $1 !^node_modules($|/)
# Rewrite everything to public except public
RewriteCond $1 !^public($|/)
RewriteRule ^(.*)$ public/$1 [L,QSA]
# 404 diret access to public
RewriteRule ^public/ - [L,R=404]
Got it working with two separate files.
.htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
# Allow node_modules
RewriteRule ^node_modules($|/) - [L]
# Rewrite everything to public
RewriteRule ^(.*)$ public/$1 [L,QSA]
</IfModule>
.htaccess inside public:
<IfModule mod_rewrite.c>
RewriteEngine on
# pass-through if another rewrite rule has been applied already
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [S=1] # Skip the next rule
RewriteRule ^ - [L,R=404]
# ...additional rules here as needed
</IfModule>
[L] can have been used instead of [S=1] in the second file if there are no additional rules.
This question gave me the idea with "pass-through if redirect".
Also thanks to #SuperDuperApps for the debugging hint with RewriteLogLevel and the original answer with the [END] flag that may have worked if I had Apache 2.4.

Apache Mod Rewrite for Pretty URLs isn't working

I'm trying to figure out how to do an apache mod_rewrite to remap $_GET.
What I'm trying to accomplish:
Currently, to get to the page one would have to go to
http://www.domain.com/index.php?URL=pages/the-page.php
I would like this to work in 2 ways:
If someone goes to domain.com/the-page, it takes them to the above but keeps it looking like this. Secondly, if someone goes to the http://www.domain.com/index.php?URL=pages/the-page.php, it will still show as domain.com/the-page, keeping the URL short and clean.
Most Recently Tried Code
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} URL=pages/([a-z0-9-_]+)\.php$
RewriteRule ^(.*) /%1
I'm pretty sure I setup everything right in the apache httpd.conf. I'm using XAMPP to test locally, restarted apache on changes, still nothing. Where am I going wrong?
I would prefer to handle this in .htaccess
I am using XAMPP localhost and trying on live server.
Log File:
127.0.0.1 - - [05/Apr/2013:16:50:43 --0400] [localhost/sid#2f3140][rid#3b14068/initial] (3) [perdir C:/xampp/htdocs/cdi/] strip per-dir prefix: C:/xampp/htdocs/cdi/index.php -> index.php
127.0.0.1 - - [05/Apr/2013:16:50:43 --0400] [localhost/sid#2f3140][rid#3b14068/initial] (3) [perdir C:/xampp/htdocs/cdi/] applying pattern '^(.*)' to uri 'index.php'
127.0.0.1 - - [05/Apr/2013:16:50:43 --0400] [localhost/sid#2f3140][rid#3b14068/initial] (1) [perdir C:/xampp/htdocs/cdi/] pass through C:/xampp/htdocs/cdi/index.php
Updated log with Olaf's script (last rule commented out)
127.0.0.1 - - [05/Apr/2013:20:02:24 --0400] [localhost/sid#2e3140][rid#3b14090/initial] (3) [perdir C:/xampp/htdocs/cdi/] strip per-dir prefix: C:/xampp/htdocs/cdi/index.php -> index.php
127.0.0.1 - - [05/Apr/2013:20:02:24 --0400] [localhost/sid#2e3140][rid#3b14090/initial] (3) [perdir C:/xampp/htdocs/cdi/] applying pattern '^' to uri 'index.php'
127.0.0.1 - - [05/Apr/2013:20:02:24 --0400] [localhost/sid#2e3140][rid#3b14090/initial] (3) [perdir C:/xampp/htdocs/cdi/] strip per-dir prefix: C:/xampp/htdocs/cdi/index.php -> index.php
127.0.0.1 - - [05/Apr/2013:20:02:24 --0400] [localhost/sid#2e3140][rid#3b14090/initial] (3) [perdir C:/xampp/htdocs/cdi/] applying pattern '^index\.php$' to uri 'index.php'
127.0.0.1 - - [05/Apr/2013:20:02:24 --0400] [localhost/sid#2e3140][rid#3b14090/initial] (2) [perdir C:/xampp/htdocs/cdi/] rewrite 'index.php' -> '/newhome?'
127.0.0.1 - - [05/Apr/2013:20:02:24 --0400] [localhost/sid#2e3140][rid#3b14090/initial] (3) split uri=/newhome? -> uri=/newhome, args=<none>
127.0.0.1 - - [05/Apr/2013:20:02:24 --0400] [localhost/sid#2e3140][rid#3b14090/initial] (2) [perdir C:/xampp/htdocs/cdi/] explicitly forcing redirect with http://localhost/newhome <--this one seems to be causing the issue
127.0.0.1 - - [05/Apr/2013:20:02:24 --0400] [localhost/sid#2e3140][rid#3b14090/initial] (1) [perdir C:/xampp/htdocs/cdi/] escaping http://localhost/newhome for redirect
127.0.0.1 - - [05/Apr/2013:20:02:24 --0400] [localhost/sid#2e3140][rid#3b14090/initial] (1) [perdir C:/xampp/htdocs/cdi/] redirect to http://localhost/newhome [REDIRECT/302]
Thank you everyone that is helping. I've spent 2 days trying to get this to work!!!
Basically, you need two rules. One rule to redirect the client to a clean URL and another to internally rewrite the pretty URL to the real content via index.php.
Assuming the index.php and .htaccess is in a directory cdi
RewriteEngine on
# prevent endless loop
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
# redirect the client
RewriteCond %{QUERY_STRING} URL=pages/(.+?)\.php
RewriteRule ^index\.php$ /cdi/%1? [R,L]
# exclude rewriting all files located in /cdi/files
RewriteCond %{REQUEST_URI} !^/cdi/files/
# rewrite to real content
RewriteRule ^.*$ /cdi/index.php?URL=pages/$0.php [L]
Update:
When the request is /cdi/index.php?URL=pages/abc.php, the second rule extracts the needed URL part and redirects the client to the new URL path. The client then requests the new URL /cdi/abc and the third rule takes this and does an internal rewrite to the real content.
This all works fine as it should, but would rewrite and redirect indefinitely. To break this endless rule, the first rule checks the environment %{ENV:...}, if the request was already redirected REDIRECT_STATUS and then stops the cycle with the RewriteRule
RewriteRule ^ - [L]
which matches everything ^ and does no substitution, but ends the rewrite cycle with the flag [L]
Instead of using the system provided environment STATUS/REDIRECT_STATUS, you can also set a variable yourself with the flag E=SEO:1 for example, and then test for this variable with
RewriteCond %{ENV:REDIRECT_SEO} 1
For the REDIRECT_ prefix, see Available Variables.
You could try this:
RewriteRule ^/([a-z0-9_-]{1,40})/?$ index.php?URL=pages/$1.php
Though ideally you might want to get rid of the "pages/" part of the query string variable, as this fixed constant could be handled by the index.php script.
You approach seems fine but your RewriteCond doesn't match your requirements:
RewriteCond %{REQUEST_URI} ^index.php?URL=pages
means "rewrite the URL if someone requests something that starts with 'index.php"—but that's not what anyone will be requesting. You want your visitors to request pretty URLs.
If your server only needs to serve those requests for /the-page, you can drop the condition entirely. Then any URL will be rewritten. (Note: This might not be what you want!)
Otherwise, the condition should read something like this:
RewriteCond %{REQUEST_URI} ^[a-z0-9-_]{1,40}
If you don't want to mess with regular expressions, you could also try this:
RewriteCond %{REQUEST_FILENAME} !-f
which means "if the user requests a URL for which no file can be found, rewrite the URL according to the upcoming RewriteRule."
If you want the group ([0-9]+) to be alphabetic then just change it to ([a-z]+) and if you've wanted it to be alphanumeric, then change it to ([a-z0-9]+), and ([a-z0-9-_]+) if with a hyphen and an underscore. If you've wanted it to set their limits manually, you can do that with this format ([a-z0-9-_]{1,40}). Do you see, the plus sign is gone, for it limited the [chars] with 1 to anything, and the {1,40} limited the [chars] with 1 to 40, you can either change it.
Do you know what the real problem is? Is my stress.. Imagine even I know that you want to remap /$var into /index.php?URL=pages/$var.php I'm still trying giving you a wrong information that will rewrite /index.php?URL=pages/$var.php into /$var. I just have realize that after my 4 hours sleep. Did you see what's happening when the time of your sleep isn't right? Maybe a rule I would gives to you when my brain's in functioning well, was:
RewriteRule ^([a-z0-9-_]+)/?$ /index.php?URL=pages/$1.php
Why did the viewers letting this to happened.. My previous codes are needed to be voted down.

htaccess rewrite trouble

I'm completely stuck with htaccess rewrites not working on my server. It's a basic index.php rewrite for Codeigniter, and works fine when developing on my Mac but fails on my server (CentOS, Apache 2, WHM)
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Attempting to load /about/ will display the index.php page. /index.php/about will display the correct page. I've tried every combination of options I can think of to no avail. A sample of my rewrite.log is below.
[dev.tirius.co.uk/sid#5d65288][rid#5e37c00/initial] (3) [per-dir /home/tirius/subdomains/dev/public_html/] add path info postfix: /home/tirius/subdomains/dev/public_html/about -> /home/tirius/subdomains/dev/public_html/about/
[dev.tirius.co.uk/sid#5d65288][rid#5e37c00/initial] (3) [per-dir /home/tirius/subdomains/dev/public_html/] strip per-dir prefix: /home/tirius/subdomains/dev/public_html/about/ -> about/
[dev.tirius.co.uk/sid#5d65288][rid#5e37c00/initial] (3) [per-dir /home/tirius/subdomains/dev/public_html/] applying pattern '^(.*)$' to uri 'about/'
[dev.tirius.co.uk/sid#5d65288][rid#5e37c00/initial] (2) [per-dir /home/tirius/subdomains/dev/public_html/] rewrite about/ -> /index.php/about/
[dev.tirius.co.uk/sid#5d65288][rid#5e37c00/initial] (1) [per-dir /home/tirius/subdomains/dev/public_html/] internal redirect with /index.php/about/ [INTERNAL REDIRECT]
[dev.tirius.co.uk/sid#5d65288][rid#5e3f7c8/initial/redir#1] (3) [per-dir /home/tirius/subdomains/dev/public_html/] add path info postfix: /home/tirius/subdomains/dev/public_html/index.php -> /home/tirius/subdomains/dev/public_html/index.php/about/
[dev.tirius.co.uk/sid#5d65288][rid#5e3f7c8/initial/redir#1] (3) [per-dir /home/tirius/subdomains/dev/public_html/] strip per-dir prefix: /home/tirius/subdomains/dev/public_html/index.php/about/ -> index.php/about/
[dev.tirius.co.uk/sid#5d65288][rid#5e3f7c8/initial/redir#1] (3) [per-dir /home/tirius/subdomains/dev/public_html/] applying pattern '^(.*)$' to uri 'index.php/about/'
[dev.tirius.co.uk/sid#5d65288][rid#5e3f7c8/initial/redir#1] (1) [per-dir /home/tirius/subdomains/dev/public_html/] pass through /home/tirius/subdomains/dev/public_html/index.php
As you can see, the htaccess is picked up and mod_rewrite is enabled and working, but nothing seems to be being appended to index.php
This must be an Apache configuration issue but I'm really lost as to what it could be.
Sample from httpd.conf
<Directory "/">
Options ExecCGI FollowSymLinks IncludesNOEXEC Indexes MultiViews SymLinksIfOwnerMatch
AllowOverride All
</Directory>
This is my .htaccess on CentOS:
RewriteCond $1 !^(index\.php|images|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php?/$1 [L]
Note the question-mark before the forward-slash.
I think the reason for this relates to running PHP as FastCGI. I believe the questionmark makes the preceding character optional (i.e. it would also match index.ph). I've tried researching this a bit more but couldn't find a definitive reason. I think I just tried every variation on the rewriterule I could find when I was looking and didn't bother asking why it worked.
Glad to be of help.

.htaccess ignore query_string adword

I'm trying to ignore a google adword get that is coming through.
Url coming in as:
/location/&gclid=287ejek22kj
This is going to a 404 page because of the gclid...
I need it to go to:
/location
I've tried this, with no success:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)&gclid=(.*)$ $1 [L]
All help is appreciated. Thanks.
Works here:
192.168.1.2 - - [01/May/2011:05:38:48 +0100] [192.168.1.2/sid#938b98][rid#2789a40/initial] (3) [perdir C:/HTTP/htdocs/] strip per-dir prefix: C:/HTTP/htdocs/location/&gclid=287ejek22kj -> location/&gclid=287ejek22kj
192.168.1.2 - - [01/May/2011:05:38:48 +0100] [192.168.1.2/sid#938b98][rid#2789a40/initial] (3) [perdir C:/HTTP/htdocs/] applying pattern '^(.*)&gclid=(.*)$' to uri 'location/&gclid=287ejek22kj'
192.168.1.2 - - [01/May/2011:05:38:48 +0100] [192.168.1.2/sid#938b98][rid#2789a40/initial] (2) [perdir C:/HTTP/htdocs/] rewrite 'location/&gclid=287ejek22kj' -> 'location/'
Check your rewrite log. See RewriteLog and RewriteLogLevel.
Looks alright but you can also try this:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteRule ^([^&]+)&gclid=.*$ /$1 [L,NC,R=301]

Apache mod_rewrite a subdomain to a subfolder (via internal redirect)

I'm trying to write a set of mod_rewrite rules that allow my users to utilize a single folder for doing development on different projects, and not have to mess with adding vhosts for every single project.
My idea to accomplish this, is to set up a "Global VHost" for every single user who needs this ability (only 3-4), the vhost would be something like: .my-domain.com. From there, I want to promote my users to write code as if it were on a domain, and not in a sub folder. For example, if bob was working on a project named 'gnome,' I'd like the URL bob (and anyone else on our internal network) loads to get to this project to be: http://gnome.bob.my-domain.com. But, what I'd like Apache to do, is recognize that "gnome" is a "project" and thus map the request, internally, to bob.my-domain.com/gnome/.
I've got what I thought would work, and it's quite simple, but..it doesn't work! The request just goes into an infinite loop and keeps prefixing the sub domain onto the re-written request URI.
The mod rewrite code i have is:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.]+)\.bob\.my-domain\.com
RewriteCond %{REQUEST_URI} !^/%1.*
RewriteRule ^(.*)$ /%1/$1 [L]
I've googled around a bit about this, but I've yet to find any real solutions that work. Has anyone tried this - or maybe, does anyone have a better idea? One that doesn't involve making a virtual host for every project (I've got designers..I think everyone would agree that a designer shouldn't be making virtual hosts..)
Thanks!
Here is a snippet from the rewrite_log:
[rid#838dc88/initial] (3) [perdir /home/bob/http/] strip per-dir prefix: /home/bob/http/index.html -> index.html
[rid#838dc88/initial] (3) [perdir /home/bob/http/] applying pattern '^(.*)$' to uri 'index.html'
[rid#838dc88/initial] (4) [perdir /home/bob/http/] RewriteCond: input='gnome.bob.my-domain.com' pattern='^([^.]+)\.bob\.my-domain\.com' => matched
[rid#838dc88/initial] (4) [perdir /home/bob/http/] RewriteCond: input='/index.html' pattern='!^/%1.*' => matched
[rid#838dc88/initial] (2) [perdir /home/bob/http/] rewrite 'index.html' -> '/gnome/index.html'
[rid#838dc88/initial] (1) [perdir /home/bob/http/] internal redirect with /gnome/index.html [INTERNAL REDIRECT]
[rid#8392f30/initial/redir#1] (3) [perdir /home/bob/http/] strip per-dir prefix: /home/bob/http/gnome/index.html -> gnome/index.html
[rid#8392f30/initial/redir#1] (3) [perdir /home/bob/http/] applying pattern '^(.*)$' to uri 'gnome/index.html'
[rid#8392f30/initial/redir#1] (4) [perdir /home/bob/http/] RewriteCond: input='gnome.bob.my-domain.com' pattern='^([^\.]+)\.bob\.my-domain\.com' => matched
[rid#8392f30/initial/redir#1] (4) [perdir /home/bob/http/] RewriteCond: input='/gnome/index.html' pattern='!^/%1.*' => matched
[rid#8392f30/initial/redir#1] (2) [perdir /home/bob/http/] rewrite 'gnome/index.html' -> '/gnome/gnome/index.html'
[rid#8392f30/initial/redir#1] (1) [perdir /home/bob/http/] internal redirect with /gnome/gnome/index.html [INTERNAL REDIRECT]
[rid#8397970/initial/redir#2] (3) [perdir /home/bob/http/] add path info postfix: /home/bob/http/gnome/gnome -> /home/bob/http/gnome/gnome/index.html
This is just a snippet, there are a few 10s or 100 or so lines of apache basically rewriting /gnome/index.html to /gnome/gnome/gnome/gnome/gnome/index.html, etc before apache hits its rewrite limit, gives up, and throws error 500
After a few years of ignoring this problem and coming back to it at various points, I finally found a workable solution.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^.]+)\.bob\.my-domain\.com
RewriteCond %1::%{REQUEST_URI} !^(.*?)::/\1/
RewriteRule ^(.*)$ /%1/$1 [L]
What I found was that back-references for previous RewriteCond directions are not available in the ConditionPattern parameter of future RewriteConditions. If you want to use a back-reference from a previous RewriteCond directive, you can only use it in the TestString parameter.
The above directives prepend the sub-domain matched in the 1st RewriteCond directive to the RequestURI, delimited by ::. What we then do in the RewriteCond Test String (regex) is re-capture the sub-domain name, then check to make sure our actual RequestURI doesn't begin with that sub-domain as a folder using a back reference within the same regex.
This sounds a lot more confusing than it really is, and I can't take the credit for discovering the answer. I found the answer as a response to another question here, %N backreference inside RewriteCond. Thanks to Jon Lin for answering that question, and unknown to him, my question too!
You might want to check
http://httpd.apache.org/docs/2.2/vhosts/mass.html
it deals with the DocumentRoot problem that you were experiencing.
Rule goes something like this
VirtualDocumentRoot /var/www/%1/
You can change the %1 for whatever suits you (http://httpd.apache.org/docs/2.0/mod/mod_vhost_alias.html)
Cheers
Some Questions:
You said "map internally" -- do you NOT want to use a redirect?
Are you using the same VirtualHost for gnome.bob.mysite.com and bob.mysite.com
Did you remember to create a ServerAlias for *.bob.mysite.com?
Here is a rough version that you could modify to work. It will capture the subdomain and requested URL, and do a redirect to the main domain with the subdomain as the first part of the path, followed by the requested path, followed by the query string.
ServerName www.mysite.com
ServerAlias *.mysite.com
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9-]+)\\.mysite.com$
RewriteRule ^/(.*) http://www.mysite.com/%1/$1 [R=301,L]',
Have you tried using another rewrite rule to process the one before it?
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.]+)\.bob\.my-domain\.com
RewriteCond %{REQUEST_URI} !^/%1.*
RewriteRule ^(.*)$ /%1/$1 [C]
RewriteRule ^/(.*)\.bob\.my-domain\.com/(.*) /$1/$2 [L]
But I think your bigger problem is the fact that your server doesn't understand it is getting served under a different name.
It thinks it is running in the /gnome/ directory while the browser things it is running in the / directory. So any relative URL's that you have are going to cause issues.
What you need is a filter that will run all the URL's in your page through a processor and change them from /gnome/ to /.