mod_rewrite espression for multi client application - mod-rewrite

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.

Related

rewrite url only and stay on the same page

I have read a lot on stack about rewriterule and how it applies and I've tried reading up on some good articles online but I still cannot wrap my head around a few things.
I have blogs setup where all folders are in
https://domain.ca/posts/post-tree/*
So I've setup htaccess like this
RewriteRule ^posts/post-tree/(.*)$ /index.php?$1 [R=301]
As I'm sure you can guess this basically brings me root index.php where I catch this request with a $_GET to know the name of the blog folder it was requesting.
This is fine I can hit index.php and with $_GET I know the blog page they requested.
What I do not get, and I've tried a lot of things, is once I have this request in index.php how do I re-write the URL to show something like https://domain.ca/blogpage/ instead of looking like https://domain.ca/index.php? where https://domain.ca/blogpage/ does not really exist of course, but it is because I want to hide the http://domain.ca/posts/post-tree/ path.
Its a little like when wordpress processes a blog page with the id and after rewrites the url to whatever slug is set for that blog page. at least my understanding of it as they don't have individual folders for blogs, but I do.
I finally got this working with the following in the htaccess file
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# the above checks if file or folder exists, if not the below is processed
# this will route to base index file and fetch $1 folder via $_GET
RewriteRule . /posts/post-tree/index.php?$1

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.

Use a sub domain to act as a folder

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.

How to redirect url to another

OK...
I have setup things so that when the following page is requested (browser users and servers)
http://www.visualise.ca/?_escaped_fragment_=corona
the website returns the following the content of this (HTML snapshot)
http://www.visualise.ca/corona
Where 'corona' always change, it varies depending on the page the users or servers are requesting. It could also be
http://www.visualise.ca/?_escaped_fragment_=anne-au-cherry
redirecting to
http://www.visualise.ca/anne-au-cherry
Thanks
UPDATE: OK let me be more clear. I use AJAX to load my Wordpress post and they appear like this http://www.visualise.ca/#!/corona when loaded. But it's not crawlable by Google that request to serv them as http://www.visualise.ca/?_escaped_fragment_=corona so I modified Wordpress to do so. Now Google can crawl my page and index its content and accessing the HTML snapshot available at http://www.visualise.ca/corona.
The problem is that when I paste the http://www.visualise.ca/#!/corona link to facebook it seems to read the http://www.visualise.ca/?_escaped_fragment_=corona and is unable to read the content. But when I paste directly the http://www.visualise.ca/?_escaped_fragment_=corona link it works, it reads http://www.visualise.ca/corona (The HTML).
So I thought maybe if I could redirect http://www.visualise.ca/?_escaped_fragment_=corona to http://www.visualise.ca/corona it would solve my problem.
Here is the existing .htaccess file
#--- DH-PHP handlers ---
AddHandler fastcgi-script fcg fcgi fpl
AddHandler php-fastcgi .php
Action php-fastcgi /cgi-bin/dispatch.fcgi
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The common mistake that a lot of people do is trying to match whole URL including query string. The reality is: when matching URL, the pattern get applied to path part of it and query string has to be matched separately.
Use this rule: it will issue 301 Permanent Redirect from this kind of URL /?_escaped_fragment_=corona to /corona (where corona can be anything).
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=([^&]*)
RewriteRule .* http://%{HTTP_HOST}/%1? [R=301,L]
I guess your question is really "how do I do this kind of redirection?". So the answer is here:
There are three ways that I could think of, each with slight differences.
Doing the redirection server side
This basically means you set redirect headers in your response. In php, you could do this using the header function. It can also do a delayed redirect, in which case you need to worry about the contents of the page.
Doing it using client side using html's "http-equiv" meta tags. This way the page always gets loaded. Example here.
doing it via javascript. Thats you basic document.location.href thing. You need to figure out a way to pass the argument to javascript, or have your JS read it from the address url itself.
Since I've shown you 3 ways of doing this, I really hope that's what you're looking for :P
Update after seeing the comments:
The above methods will cause the URL to change. If you don't want the URL to change, but show the contents of that other page on your original page, you caould either do that using iframes (baaaad), or do the decent thing and set up URL rewriting.
:)
RewriteEngine on
RewriteRule \?_escaped_fragment_=(.*?) /$1 [L,R=301]
is it what are you looking for?

URL rewriting problem

In order to convert dynamic URLs on my site www.kitesmovie.co.in to static urls. Eg: www.kitesmovie.co.in/stories.php?id=10 to www.kitesmovie.co.in/Barbara_Mori_Hrithik_Roshan_New_Movie.
I tried using rewriting rules in my htaccess files, but it did not work. Please tell me how to do this.
Thanks a lot in advance.
It's not really clear what you are trying to achieve.
When you say "convert dynamic urls to static urls", do you really want to have user to type in the ...?id=10 and the file named Barbara_Mori_Hrithik_Roshan_New_Movie lives on your server? I think, it's the opposite way - you want to let the user type in the long & nice title and actually resolve it to ?id=10
If this is the case, the #Mike's answer is nearly correct, you only have to swap the parts of the last line:
RewriteRule Barbara_Mori_Hrithik_Roshan_New_Movie stories.php?id=10 [R,L]
Another question - are you sure you have the .htaccess working? An easy way to check is to set the contents of .htaccess to
order deny, allow
deny from all
And see if you can still access that directory. If you can, this means the "deny from all" does not work. Then check your apache config - is .htaccess allowed in the particular virtualhost and/or directory.
Lots of missing info there. If you are using apache with mod_rewite installed, place this in your .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule stories.php?id=10 Barbara_Mori_Hrithik_Roshan_New_Movie [R,L]
</IfModule>
You might want to have a look at this article: http://www.addedbytes.com/for-beginners/url-rewriting-for-beginners/
Generally, you would use a RewriteRule something like this:
RewriteRule ^stories.php?id=10$ Barbara_Mori_Hrithik_Roshan_New_Movie [R=301,L]
If that's not working for you, then feel free to add more details to your question.

Resources