Use a sub domain to act as a folder - mod-rewrite

I have this code:
# Use WordPress subdomain
RewriteCond %{HTTP_HOST} domain.co.uk
RewriteCond %{REQUEST_URI} ^/wp/(.*)$
RewriteRule .* http://wp.domain.co.uk/%1 [L]
This works, exactly how I want it to, so when you go to domain.co.uk/wp/index.php it shows the page wp.domain.co.uk/index.php.
However, rather than it redirecting you in the browser to the physical address wp.domain.co.uk/index.php I would like it to display domain.co.uk/wp/index.php.
Is that possible?
Thanks,
Ian

If the domain.co.uk and wp.domain.co.uk domains don't share a common document root (or if the latter isn't in a subdirectory of the former), you'll need to rely on mod_proxy. This won't work unless you have mod_proxy enabled. You'll just need to add a P flag to your RewriteRule:
RewriteRule .* http://wp.domain.co.uk/%1 [L,P]
Otherwise, if they share the same document root, just remove the http://wp.domain.co.uk and point it to the appropriate place within your document root.

Related

Single instance of Joomla!, subdirectories and multiple domains

We recently moved a number of static websites from multiple (regional) domains onto a single .com domain which uses Joomla! to serve up content. The new site uses subdirectories and allows uses to navigate between countries. Like this:
newdomain.com/country-name1
newdomain.com/country-name2
newdomain.com/country-name3
We would now like each site to go back to having it’s own domain, but to essentially serve up the same website the user would be seeing by viewing the sub directory (we’ll probably drop the ability to navigate between countries, back that’s largely irrelevant to this post).
How can we do this with as little work as possible to the templates whilst retaining a single Joomla! instance? Has anyone got any experience of similar? I've read some articles but am not sure any of them give the user a true sense of being on a separate domain. I could of course be wrong (tbh, this is a little out of my field of expertise). Spoon-feeding appreciated. :)
You could try mapping the requests to the relevant folders
RewriteCond %{HTTP_HOST} ^(www\.)?newdomain\.com [NC]
RewriteRule ^/(.*) /country-name1/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?newdomain\.fr [NC]
RewriteRule ^/(.*) /country-name2/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?newdomain\.de [NC]
RewriteRule ^/(.*) /country-name3/$1 [L]
That should map a request for
newdomain.com/country-name1/somepage to newdomain.com/somepage
newdomain.com/country-name2/page to newdomain.fr/page
etc
Obviously you'd also have to make all domain names resolve to the same folder.

when to use which mod_rewrite rule for self routing?

There are several ways to write a mod_write rule for self routing. At the moment i am using this one:
RewriteCond %{REQUEST_URI} !\.(js|ico|gif|jpg|png|css)$
RewriteRule ^.*$ index.php [NC,L]
But i also could use
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php
OR
ErrorDocument 404 /index.php
There may be many more.
Are there any drawbacks for using one of these examples?
Are there any use cases where one rule makes more sense then the other?
Could you explain the difference between these rules in detail?
Thx for your time and help.
When your condition is:
RewriteCond %{REQUEST_URI} !\.(js|ico|gif|jpg|png|css)$
Then only images, icons, styles, and javascript are excluded from routing. This means you can't access static html, directories, or directory indexes. So if you just want to plop down a static html page somewhere, and serve it without it getting routed through index.php. It also means if you accidentally put an image or script or style in the wrong place, and try to access it (you would normally get a 404), it wouldn't get routed through index.php eventhough and would yield the default 404 error page.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
These conditions will exclude any URI that points to an existing resource. So if you plot an image, a script, or directory, static html, etc anywhere in your document root, you'll be able to go there without it being routed through index.php. Sometimes the condition RewriteCond %{REQUEST_FILENAME} !-s is also included, which excludes URI's that point to a symlink. This is usually what you'd see when doing routing, wordpress uses this.
ErrorDocument 404 /index.php
This does essentially the same thing as the previous conditions, except it does it outside of mod_rewrite and there's no way to impose additional conditions in the future or as needed. The downside of doing routing outside of mod_rewrite is that mod_rewrite and the core directives (ErrorDocument in this case) do processing on the URI at different times in the URI-file mapping pipeline. So if you have rules that do other things, they could get applied, and then ultimately still get routed through index.php because the 2 directives are conflicting with each other. Simply because rewrite rules are applied at one point in the pipeline doesn't mean other directives won't get applied later down in the pipeline. This is a bad way to do routing.
There's also stuff like:
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^.*$ index.php [L]
Which will blindly route everything. Even javascript, even images, even static html, everything. Sometimes this is what people want. Ultimately, this is going to be dependent on what you want and what your index.php script does. Is it going to handle 404's? (like what you'd want in the first routing rule), is it just going to handle non-static resources? (like what the second rule does), or is it a literal catch all and will do everything (what the rule above does)?
Also note that your rewrite flags are different between the first and second rules. Those are significant if you have other rules.
The biggest drawback to the first example (which is the one you say that you use) is that this method hard codes the files extensions (.js .ico .gif .pnd) that are excluded from being rewritten to index.php. The problem with this is that if you need to add new static content that uses a file extension that is not in your exclusion list, you must modify your rewrite rule accordingly. For example, if you were to start hosting flash content and needed to host .swf and .flv files you will need to update your existing rewritecond rule.
The middle solution is best (IMHO) because it does exactly what is says it does, namely if the requested file doesn't exist (!-f condition) OR the requested directory doesn't exist (!-d condition) then rewrite the request to index.php.

mod_rewrite espression for multi client application

I have a php application that serves multiple customers. Code is placed in the domain's root and is shared for all customers. Each customer can access it's page by using query string parameter "id".
I need advice and a sample code how to achieve this routing via mod_rewrite or it' better way to do it through php routing script:
Home page:
www.example.com/customerA --> www.example.com/customerA/main?id=1
www.example.com/customerB --> www.example.com/customerB/main?id=4
Note: "main" is main.php file not displaying file extensions.
Customer subfolders are not the real ones.
Inner pages are using additional parameters like:
www.example.com/customerA/page1?id=1&par1=5
On SERVER SIDE all rewrites should be interpreted as www.example.com/main?id=4
without virtual subfolder.
Thanks.
Here's what should work:
RewriteRule ^/customerA /customerA/main?id=1 [QSA,NC,R=301,L]
RewriteRule ^/customerB /customerB/main?id=4 [QSA,NC,R=301,L]
RewriteRule ^/(customer(A|B))/main /main [QSA,NC]
Now that's I've answered precisely to your question, I'm pretty sure it's not what you want.
If you have a lot of customers, I've made a huge answer here of a question that was about films, but you it's about customers, but the principle is exactly the same.
If you want to be more generic:
# if URL is not a real file...
RewriteCond %{SCRIPT_FILENAME} !-f
# if URL is not a real folder...
RewriteCond %{SCRIPT_FILENAME} !-d
# ...and if adding "php" points to a real file...
RewriteCond %{SCRIPT_FILENAME}.php -f
# ...then rewrite internally with "php" extension:
RewriteRule (.*) $1.php [QSA,NC]
Hope this helps.

dynamic subdomains with htaccess: URL shouldnt change in the browser

Trying to implement subdomains with htaccess.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.
RewriteCond %{HTTP_HOST} ^([a-z0-9]+)\.domain.com(.*)$
RewriteRule ^(.*)$ http://domain.com/index.php?/public_site/main/%1/$1 [L]
</IfModule>
when i enter ahser.domain.com the browser URL is changing. is there a htaccess option to not let this happen when absolute URLs is used in RewriteRule?
Don't rewrite to a full URL with domain in it. That generates a redirect since it's going to a different website! You could put microsoft.com there; so how would it work without redirecting?
What you have to do is make sure that the web pages work under the original domain. So when the client asks for myname.domain.com/... how about rewriting that to myname.domain.com/index.php?public_site/main/myname/.... Keep the domain the same. The index.php? can be made to work in any of those domains. For instance, even this could work:
http://OTHER.domain.com/index.php?public_site/main/MYNAME/...
I.e. set it up so it doesn't matter which virtual host accesses that path.
Once you have that, the rewrite can then just do:
# will not trigger redirect
RewriteRule ^(.*)$ /index.php?/public_site/main/%1/$1 [L]
You have to be careful not to introduce a loop since you're now redirecting a URL to a longer URL which matches the same rewrite rulethe same domain. You need an additional RewriteCond not to apply this rewrite if the URL already starts with /index.php?public_site/.

htaccess rewrite

I would like to rewrite /anything.anyextension into /?post=anything.
eg:
/this-is-a-post.php into /?post=this-is-a-post or
/this-is-a-post.html into /?post=this-is-a-post or even
/this-is-a-post/ into /?post=this-is-a-post
I tried
RewriteRule ^([a-zA-Z0-9_-]+)(|/|\.[a-z]{3,4})$ ?$1 [L]
but it doesn't work.
Any help appreciated.
If you have access to the main server configuration, use this:
RewriteRule ^/(.+)\.\w+$ /?post=\1 [L]
If not, and you are forced to put this in a .htaccess file, you could try
RewriteRule ^(.+)\.\w+$ /?post=\1 [L]
In either case, this assumes you will only be rewriting URLs with a single path component (i.e. if you get a request like /path/anything.anyextension it might not work as you expect, the rewrite rule would need to be modified to handle that)
You need a better way to determine when to apply the rewrite rule, otherwise your page won't be able to display external JS or CSS, unless you define an exception.
SilverStripe (or the core, Sapphire) offers a good approach to this, something like:
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.css)|(\.js)|(\.swf)$ [NC]
RewriteCond %{REQUEST_URI} .+
RewriteRule ^([^\.]+) /?post=$1 [L,R=301]
This requires the URI not to be empty, not to be JS, CSS or SWF, and redirects back to your root directory:
http://localhost/this-is-a-post.php
http://localhost/?post=this-is-a-post
If you don't want a redirection, but the processing, remove the redirection rule R=301

Resources