Hi Dklynn,
Yes I could do that but the problem of being able to access relative URL’s on an HTTP_HOST value that is NOT the actual domain that it will be when I change the DNS to point to the new host location, would still remain. So that would not alleviate me having to either figure out how to use .htaccess to rewrite such URL’s or go through the pain of hard coding the new host (at the subdomain) for testing purposes and then change the URL’s again when the domain goes live.
When you use a subdirectory and change the level of the request, you’re confusing the visitors’ browsers when they go to look for relative links.
I think I understand your point but I am not doing this for the sake of visitors. I am doing this to try and test the site to make sure everything works fine before going live with it.
Admittedly some visitors might come along and be a bit confused but again my aim is not to make the site URL’s super user friendly at this point in the move. It’s just to make sure everything works before making it go live.
[COLOR=#FF0000]<IfModule mod_rewrite.c>
[INDENT]...If you don't know whether
a module is enabled, run the test ONCE then REMOVE the wrapper as it is
EXTREMELY wasteful of Apache's resources (and should NEVER be allowed
on a shared server).[/INDENT]
[/COLOR]
Interesting. I’ve always used the IfModule because EVERY single piece of .htaccess code example I have EVER seen (and I mean ever seen) has used it. As such it seemed advisable to use it though I do see your point and have removed it from my code as you suggest.
Though I also wonder what will happen if my host removes the mod_rewrite module for some reason but then again that is as likely to happen as that they stop using Apache.
RewriteEngine On
RewriteBase /
# Why? This is not needed!
Again…it’s what my CMS spits out for me to put into the .htaccess file. So I have used it as such. And like with the IfModule EVERY single piece of code I have ever seen that uses the mod_rewrite module turns it on which would indicate that there is some reason for always having “RewriteEngine On” in the code.
I’d rather be safe and leave such statements in than take them out on sites that belong to clients where I want nothing to go wrong. Especially regarding code that I see included everywhere.
Incidentally I removed the “RewriteEngine On” to test out what you said and when I do that…NOTHING gets rewritten at all so I think you must be mistaken about it being okay to take it out.
RewriteCond !~username
[COLOR=#FF0000]# The syntax of the RewriteCond statement is
# RewriteCond [string to test, i.e., %{REQUEST_URI}] [string to match] [optional flags]
# Moreover, unless your ~username is fixed (a constant), you will need to change this
# value and a RewriteCond statement is not the place to do that!
[/COLOR]Well…the ~username is a constant in the sense that EVERY URL on the site must include it as part of the URL. So I am not sure what you mean by saying that I will need to change this value.
I do see where my example code was missing something in the RewriteCond.
It should have been “RewriteCond %{REQUEST_URI} !username [NC]”. In other words I am testing to see if “username” is part of the URL. If it’s not then and only then do I want the RewriteRule to execute.
RewriteRule ^(.*)$ /~username/$1
[COLOR=#0000FF]# Wasteful - consider testing the "it doesn't match above" [I]then[/I]
# RewriteRule .? ~username%{REQUEST_URI} [L][/COLOR]
Now that is a very interesting way to do it. I will definitely use that!
I am not quite sure what the “.?” stands for though I know question marks are often used to indicate non-greedy and the dot of course is any character I believe. But I am just not sure why you used the .? is all.
The following is what I have thus far…but it’s still not working correctly. It works as desired on my localhost copy of Apache but for some reason when I transfer this code to my host it continues to revert to a host of “http://account12617.hosting.com/” without the ~username included in the URL.
Whereas when I run the exact same code on my local copy of Apache it correctly rewrites all URL’s that are missing the ~username as “http://account12617.hosting.com/~username/”.
Not sure why that difference in result yet.
Here’s my code…
RewriteCond %{HTTP_HOST} !^somedomain.com\\.local$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !username [NC]
RewriteRule .? ~username/index.php%{REQUEST_URI} [R,E]
somedomain.com.local is just a local copy of the site that I run locally for testing and development.
http://account12617.hosting.com/~username/ is the temporary URL given to me by the host.
When the site goes live it will simply be http://somedomain.com (without the .local ending).
Thanks again for your input! Much appreciated.
Carlos