Forum Moderators: phranque

Message Too Old, No Replies

multilingual url rewrite

multilingual url rewrite htaccess

         

kockhwie

8:22 am on Nov 16, 2010 (gmt 0)

10+ Year Member



Hi,

How to redirect to

www.abc.com/application/home/index.php?lang=en&action=test
from

www.abc.com/application/en/home/test
by using htaccess?

Others:
www.abc.com/application/about/index.php?lang=en&action=try
www.abc.com/application/ or www.abc.com/application (without end "/")


The application might have few different languages, so if possible i do not want to hardcoded. how do i work it out using htaccess & php?

Thanks!

jdMorgan

2:40 pm on Nov 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What have you tried so far? We can help you get your code working here, but we can't write your code for you: Due to a very limited number of contributors here, we simply cannot offer a "free global code-writing service." The resources in our Apache Forum Charter and the examples in our Apache Forum Library may be quite useful to get you started. Links to both are at the top of this page.

It will be possible to extract the language parameter from the query string and use it as a "back-reference" to create the new URL to which you wish to redirect. So it will not be necessary to 'hard code' a rule for each language case. If desired, the 'test' parameter may be similarly back-referenced to allow the same rule to be used for other query-driven "functions." As this "back-reference" concept is key to achieving your goal, pay particular attention to it when you see it in the cited resources and in the Apache mod_rewrite documentation.

Also, it is often the case that posters here confuse external redirects with internal rewrites and frequently misunderstand the "direction" of mod_rewrite's redirect and/or rewriting action; Be aware that mod_rewrite takes action after a URL is requested by an HTTP client (e.g. browser or search engine robot) from your server, and before any server content is sent in response to that client request. For this reason, mod_rewrite cannot "change the URL" which has already been requested.

Therefore, I should ask you what URL you intend to publish on your Web pages, because that URL is the one which will be listed by search engines and which will appear in the users' browser address bar, and mod_rewrite acts too late in the process to change that URL.

Most of the time, posters here are trying to implement so-called "search-engine-friendly static" URLs, in which case the usual procedure involves three steps, the last of which is optional. Note in particular the distinction between redirects and rewrites, and the 'direction' of these functions:

1) Change all URLs published on your pages from the dynamic form (e.g. www.example.com/application/home/index.php?lang=en&action=test) to the static form (e.g. www.example.com/application/en/home/test). This is done by editing the HTML source for each static HTML page (if those pages are static) or by modifying the script which generates those HTML pages (if they are generated dynamically.)

2) Create a RewriteRule to internally rewrite (not redirect) client requests for the static SEO-friendly URLs to the corresponding internal script filepath plus query string. This internal rewrite is a URL-to-filepath translation.

3) (Optional) Create a RewriteRule to externally redirect only direct client requests for the (old) dynamic URLs to the (new) static URLs to speed up search engines' re-indexing of the new SEO-friendly URLs. This external redirect is a URL-to-URL translation.

The "direct client requests only" proviso in the final step is required in order to avoid an 'infinite' rewrite/redirect loop which will effectively make both the static and dynamic URLs inaccessible, and may take down your server if your site is very busy.

This three-step process is described in some detail in the thread "Changing Dynamic URLs to Static URLs - Implementing search engine friendly URLs with mod_rewrite [webmasterworld.com]", one of many threads in our Apache Forum Library.

Jim

kockhwie

4:32 pm on Nov 16, 2010 (gmt 0)

10+ Year Member



Hi Jim,

Thanks for your explaination. And sorry about my inconsiderate action! I did try to contribute in the other boards (but not to "Apache Web Server") try to help the members/reduce the contributors work load (if my answer really help :p). because i am no that familiar with complicated rewriterule, so please forgive me (since i have show my sincerity! haha) I really appreciated your response.

I did a lot of research & tried out many ways on that, because previously i use PHP to handle these SEO user friendly URLs, one of my friends told me that mod_rewrite can do exactly the same thing. i am curious so i want to try it out (but seems it is not that humanity :p)

i had tried more than 50 solutions, & search all the similar posts from this forums for whole night, but seems like i m not that smart enough. :D

I still cant get the solution, but the following code is the most "look-alike" (haha) :


RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /home/index.php?action=$1&lang=$2 [L]
#RewriteRule ^([^/]*)/([^/]*)$ /(home|about)/index.php?action=$1&lang=$2 [L]


i still do not know how to move the language value before the directory("home").

Thanks again.

Jack

kockhwie

6:41 pm on Nov 16, 2010 (gmt 0)

10+ Year Member



i found a better one :
RewriteRule (.*)/(.*)/ index.php?lang=$1&action=$2
RewriteRule (.*)/(.*) index.php?lang=$1&action=$2

so i will get the result :
www.abc.com/application/home/en/test/
(www.abc.com/application/home/index.php?lang=en&action=test)

But still dunno how to make the language in front of the subdirectory "home"...
And curious whether i can make this done in a single htaccess file which only located in the project root (/application not each folder i have to add a htaccess)
i m quite happy now... :D
Will continue doing research by tomorrow.

Thanks!

g1smd

8:39 pm on Nov 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There are big problems with that code. The multiple (.*) patterns are very inefficient requiring hundreds of trial matches per URL request. Use a better pattern such as
([^/]+)/([^/]+)/
or similar.

You need the [L] flag on every rule.

kockhwie

6:18 am on Nov 19, 2010 (gmt 0)

10+ Year Member



hi g1smd,

Thanks. Got it!
I m curious whether i can rewrite the URL from
www.abc.com/application/home/en/test/
to
www.abc.com/application/en/home/test/

?

g1smd

7:56 pm on Nov 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You cannot rewrite a URL to another URL


You can redirect a URL to another to URL.

You can rewrite a URL request to a non-default file path.

Which do you want?

kockhwie

10:14 am on Nov 23, 2010 (gmt 0)

10+ Year Member



i want
www.abc.com/application/home/index.php?lang=en&action=test
from

www.abc.com/application/en/home/test

After i tried some codes, i can only make the rewrite to:
www.abc.com/application/home/en/test/
BUT i want the "en" (the lang) is placed in front the directory "home", possible?

Thanks

g1smd

10:34 am on Nov 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You are still confusing "rewrite" and "redirect".

You have used the word "rewrite" and then specified two URLs.

Do you want a redirect from one URL to another URL, or do you want a rewrite from an incoming URL request to an internal server filepath?

URLs and filepaths are two entirely different things. One exists only "out on the web". The other exists only "inside the server".

kockhwie

4:34 pm on Nov 23, 2010 (gmt 0)

10+ Year Member



When user enter the url : www.abc.com/application/en/home/test
it goes to :
www.abc.com/application/home/index.php?lang=en&action=test

if user enter the url : www.abc.com/application/jp/services/view/details
it goes to :
www.abc.com/application/services/index.php?lang=jp&action=view&mode=details

User wont see the "www.abc.com/application/home/index.php?lang=en&action=test" on the browser. User can only see the optimized url which is "www.abc.com/application/jp/services/view/details
"
In php, i can grab the the GET params: lang, action & mode(optional).
This so called rewrite, right?

if users try to enter(impossible users know the url style, unless they are hackers or they try to guess :P ) :
www.abc.com/application/home/index.php?lang=en&action=test

for security issue, i should send the user to homepage (right?).

p/s: I will only use the mode_rewrite method in new project, so i wont use redirect.

Thanks.

g1smd

8:37 am on Nov 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Please do not use the words "goes to". It is NOT clear what you mean. I'd assume the browser URL changes to that new URL, i.e. a redirect.

Redirects and Rewrites both use similar RewriteRule coding, but perform different functions.

If you're talking about a rewrite, is this what you mean?

If user enters the URL:
www.abc.com/application/jp/services/view/details

the user sees the server internal contents found at:
/application/services/index.php?lang=jp&action=view&mode=details


You need to read the second post in this thread. It has all the information to make this clear.

kockhwie

10:19 am on Nov 24, 2010 (gmt 0)

10+ Year Member



Yup g1smd, this is what i want.

I will go thru again the 2nd post. Thanks.

jdMorgan

11:31 pm on Nov 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Based on your code above, in order to rewrite the URL-path /application/en/<home or about>/test/ to your script at filepath /application/<home or about>/index.php?lang=en&action=test, you will need to move this rule to /application/.htaccess, include the subpattern for the language and "about or home" in the RewriteRule pattern, and include the complete path to your script in the RewriteRule substitution path.

# Internally rewrite requests for URL-path /application/<lang-code>/<script-dir>/<action-name>/
# (with or without a trailing slash) to /application/<script-dir>/index.php script
# filepath with lang and action as query string name/value pairs
RewriteRule ^([a-z]{2})/([^/]+)/([^/]+)/?$ /application/$1/index.php?lang=$1&action=$3 [L]

This one rule replaces both of your rules posted above, and incorporates the suggestions to use more-specific patterns as well as other tweaks.

Jim