How to emulate subdomain/language in .htacces file - .htaccess

I have been looking at some way to implement the language subdomain behavior of the enterprise version of GTranslate and have come close.
Basically, if you want subdomains like fr.domain.com, es.domain.com, de.domain.com, etc
for the PRO version (general implementation), then there should be a way to handle this with modifications in the .htaccess file.
This isn't quite right, but it's coming close - for example, if you wanted "de.domain.com" for German, there might be a modification like:
RewriteCond %{THE_REQUEST} !^[A-Z]+\ /gtranslate/
RewriteRule ^(.*)$ http://de.domain.com/gtranslate/translate.php?lang=de&url=$1 [NC,L,R=301]
The Condition is to prevent recursion... This seems to come close, but the problem is that the URL in the browser now shows de.domain.com/gtranslate/translate.php?lang=de&url=about.html instead of the desired de.domain.com/about.html
There must be a way to handle this so that we can emulate this behavior.
Motivation: Perhaps I should have stated this up top.
I'm using the general version with Joomla - seems to work most of the time, BUT it looses the language during a form submission because the redirection (POST?) of the form looses the language code (normally a 2-letter prefix to the URL.
If I was able to keep a language per subdomain, then it seems like I would be able to properly keep the language during a form submission (as well as the potential "niceness" of having a subdomain to reflect the language - could even be the name of the country).
Anyone who is better in mod_rewrite want to take a stab at this?

There must be a way to handle this so that we can emulate this
behavior.
No. As soon as you do a 301 this is a full redirect, so the URL is modified, and, unfortunaltely, you can't avoid this.

Related

htaccess redirect pretty URLs to ugly ones

So, I'm trying to make my URL's a bit more pretty and sharable. I have a website with some items that users can currently access with example.com/?i=itemName. However, I'd like users to be able to write example.com/itemName instead.
This means I'd have to do some redirection with htaccess. I want to redirect all URL's to example.com itself, but keep the URL the same. To clarify, an example:
User types example.com/niceItem. The server shows the content of example.com, but keeps the URL as example.com/niceItem (alternatively, it can change the URL to example.com/?i=niceItem, then I can simply read the URL with javascript and change it back to example.com/niceItem in the adress bar).
So far, this is the best I could do:
RewriteRule ^/([^\/]+)$ /index.php?i=$1 [NC,L]
The idea is to capture the requests that don't have slashes after the first one (like example.com/niceItem), and then read the file at example.com/index.php?i=niceItem. The problem is, when I load a page like example.com/niceItem, the page displays what the value of i is with php; it should be niceItem, as the link is supposed to be example.com/?i=niceItem, but the value of i is actually the string index.php. Not quite what I wanted. Also, I'd expect the following to work
RewriteRule ^/([^\/]+)$ /?i=$1 [NC,L]
but this actually causes an internal server error.
So, the question is, why do those not work, and how would I be able to achieve what I'm trying to achieve?
PS. Actually, this website I'm talking about is a subdomain of example.com. So, I have sub.example.com which maps to example.com/sub/, and I need the URL's to be prettyfied like sub.example.com/itemName or example.com/sub/itemName. As I mentioned, the format of the URL isn't that big of a deal as long as the itemName part is in there. I'll be able to read the URL with javascript and change it to whatever I want once the page has loaded.
Use RewriteCond
If i is the only query argument that will be passed then
RewriteCond "%{QUERY_STRING}" "(\?i=)(.*)$"
RewriteRule "(.*)/?$" "$1/%2"
If you need to extract i only but keep other query args
RewriteCond "%{QUERY_STRING}" "(.*(?:^|&))i=([^&]*)&?(.*)&?$"
RewriteRule "(.*)/?$" "$1/%2?%1%3"
Most every framework provides this sort functionality. It is best not to reinvent the wheel when possible. This is a fragile setup, and it will probably cause you headaches in the future.

.htaccess redirect to subfolder, and remove it's name

I'm kind of noob in the world of web so my apologies... I tried many things found on SO and elsewhere, but I didn't manage to do what I want. And the Apache documentation is... well too much complete.
Basically what I want to do is redirect my domain to a subfolder. I found easy solutions for this (many different actually).
http://www.foo.com/
http://foo.com/
should redirect to /bar and appear as http://foo.com/
Using the following I got the expected result :
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www\.foo.com$
RewriteRule ^/?$ "http\:\/\/foo.com" [R=301,L]
RewriteRule ^((?!bar/).*)$ bar/$1 [NC,L]
But I also want the subfolder as well as filenames not to appear when explicitly entered, i.e :
http://www.foo.com/index.html
http://foo.com/index.html
http://wwww.foo.com/bar
http://foo.com/bar
http://wwww.foo.com/bar/index.html
http://foo.com/bar/index.html
Should all appear as
http://foo.com/
Is this possible ?
Obviously using .htaccess, since I'm on a virtual host.
Thanks
As Felipe says, it's not really possible, because you lose information when you do that R=301 redirect: a hard redirect like this starts a whole new request, with no memory of the previous request.
Of course, there are ways to do similar things. The easiest is to put the original request in the query string (here's a good rundown on how mod_rewrite works with query strings). Sure, the query string does show up in the URL, but most modern browsers hide the query string in the address bar, so if your goal is aesthethics, then this method would be workable.
If you really don't want to show any of the original query in the URL, you might use cookies by employing the CO flag (here are some very good examples about cookie manipulation). At any rate, the information about the original request must somehow be passed in the hard redirect.
But anyhow, and most importantly, why would you want to do something like this? It's bound to confuse humans and robots alike. Great many pages behaved like this back when frames were fashionable, and it was pretty terrible (no bookmarking, no easy linking to content, Google results with the snippet "your browser cannot handle frames", no reloading, erratic back button, oh boy, those were the days).
Speaking of which, if your content is html, you may just use a plain old iframe to achieve the effect (but I'd sincerely advise against it).

.htaccess rewrite howto [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
redirect using htaccess file
Hello there (very simple question),
How to map
mydomain/language/somepage
To
mydomain/somepage.php
The physical page being of course mydomain/somepage.php
The idea is that /en/somepage, /fr/somepage, /de/somepage all point to somepage.php (I intend to get the language passed in URL with php).
More specifically I would like that apache understands to SUPPRESS either /fr /en /de from the request URI and deliver the right document, my physical tree not having any /fr /en /de subdirectory : i.e.
root/fr/subdir/subsubdir/somepage
should be pointing to
root/subdir/subsubdir/somepage.php
Thank you !
* EDIT *
So, I think I get a better idea how URL rewrites work so, I've come to this. It's broken and I actually suck at regular expressions so if somebody could help me sort it out I would love it ! ^^
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond ^[a-z]{2}$/%{REQUEST_FILENAME}\.php -f
RewriteRule ^([a-z]{2})/(.*)$ $2.php?lang=$1
Explanations : the URL will be passed to apache in the form of
root/(fr|de|en|whatever)/document
and is (should be) rewritten in the form of/passed to php
root/document.php?lang=(fr|de|en|whatever)
I hope it's clear enough, TYVM !
Also, being able to pass subdirectories as well like
root/(fr|de|en|whatever)/sub/.../document
would be very nice. I need further explanations on htaccess for constants for this like %{REQUEST_FILENAME}, %{REQUEST_URI}, ...
TY
If you are trying to redirect users to a certain URL in apache, there is this tutorial that should give you a good guideline:
Apache mod_rewrite
However, for you website, i think a better approach to take would be use the language option as a query string. I feel this way you could better handle language based sites. For example, if you have localised version of your website for 30 different countries, you will need to do a mod_rewrite for all 30 of them and when you need to update your site, you will have to fix them all. However, if you use a query string. You somepage.php could pick up the query string value and based on that render the necessary page.
I think it is easier to implement. (Or maybe it is a wrong way of doing it and I am unaware of the short-comings)

301 redirect question?

Is this qood example of redirection of page to another domain page:
RewriteCond %{HTTP_HOST} ^dejan.com.au$ [OR]
RewriteCond %{HTTP_HOST} ^www.dejan.com.au$
RewriteRule ^seo_news_blog_spam\.html$ "http\:\/\/dejanseo\.com\.au\/blog\-spam\/" [R=301,L]
or good old works too:
301 redirect seo_news_blog_spam.html http://dejanseo.com.au/blog/spam/
and whats the difference?
Presumably, the rules are functionally equivalent (well, assuming that http://dejanseo.com.au/blog/spam/ was supposed to be http://dejanseo.com.au/blog-spam/ like the first one redirects to, and the only host pointing at that location is dejanseo.com.au with or without the www).
The first example uses directives from mod_rewrite, whereas the second one uses some from mod_alias. I imagine that the preferred option is the second one for this particular case, if not only because it's a bit simpler (there's also marginal additional overhead involved in creating the regular expressions being used by mod_rewrite, but that's very minor):
Redirect 301 seo_news_blog_spam.html http://dejanseo.com.au/blog-spam/
However, I suspect the reason that you have the first one is that it was created using CPanel (based on the unnecessary escapes in the replacement that appeared before in another user's question where it was indicated CPanel was the culprit). They've gone with the mod_rewrite option because it provides conditional flexibility that the Redirect directive does not, and I assume this flexibility is reflected somewhat in whatever interface is used to create these rules.
You'll note that there is a condition on whether or not to redirect based upon your host name in the first example, identified by the RewriteCond. This allows for you to perform more powerful redirects that are based on more than just the request path. Note that mod_rewrite also allows for internal redirects invisible to the user, which mod_alias is not intended for, but that's not the capacity it's being used in here.
As a final aside, the host names in your RewriteCond statements should technically have their dots escaped, since the . character has special meaning in regular expressions. You could also combine them, change them to direct string comparisons, or remove them altogether (since I imagine they don't do anything useful here).
Unbeliavable, the problem was that the synthax wasn't correct, so instead of:
redirect 301 seo_news_blog_spam.html http://dejanseo.com.au/blog/spam/
it should look like this:
Redirect 301 seo_news_blog_spam.html http://dejanseo.com.au/blog/spam/
One, first big letter was the source of all troubles, what a waste of time :D
it works now as it supposed to!
Thanks to everyone who participated, issue solved.

htaccess rewrite question

I need to redirect incoming requests for a page to a subdirectory whilst keeping the URL displayed as originally typed.
This is to manage language pages easier.
For example, I want Spanish pages to be under www.mydomain.com/es. I want the URL displayed to remain www.mydomain.com/es.
But the actual page is held here - www.mydomain.com/international/es
This means I can keep my root folder tidy and have all the language pages in one directory. But I want it to be easy for language specific visitors to find thier page.
Can anyone point me in the right direction? I've had a go but to be honest I always manage to produce server errors and get in a mess.
Many thanks
TT
Provided you are on an apache server look up mod_rewrite.
Example for your .htaccess:
RewriteEngine on
RewriteRule ^es/([0-9a-zA-Z\/]*)$ /international/es/$1 [L]

Resources