First time here, and I’m a bit of a mod_rewrite and regex noob, so please let me know if I’ve missed anything. I’ve searched here and elsewhere all day but not found exactly what I need.
I’m looking for a set of RewriteConds and RewriteRules that will accomplish the following:
- remove the www, if present in URL
- redirect into a folder, if not already present in URL
- preserve the rest of the URL
I have a web app installed in a specific subfolder (we’ll call it /webapp) which is configured to require no www on the url. It presents a stupid annoying message to the user if they include the www. I can dig into the app and reprogram that, but I’d like to handle that for the users through .htaccess and mod_rewrite, and simultaneously dump them into the folder, if they forget to type it, doing all of this with a 301 redirect.
For example, I’d like any of the following requests
http://www.mydomain.org/webapp/anything
http://www.mydomain.org/anything
http://mydomain.org/anything
To be redirected to
http://mydomain.org/webapp/anything
And obviously if a “correct” URL (one starting with http://mydomain.org/webapp/) is requested, it’s not rewritten at all.
My best guess so far is as follows:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.mydomain\.org$ [NC]
RewriteRule ^(.*)$ http://mydomain.org/$1 [R=302]
RewriteCond %{REQUEST_URI} !^/webapp.*$ [NC]
RewriteRule ^(.*)$ http://mydomain.org/webapp/$1 [R=302]
This seemed to work according to http://htaccess.madewithlove.be/ but in practice, not so much.
Thanks in advance.
Found the answer myself here: BowlerHat
Looks like this:
I decided that if users try to visit mydomain.org/somethingsomething, just to send them to the root of the webapp, rather than /webapp/somethingsomething.