Forum Moderators: phranque

Message Too Old, No Replies

Add string to URL via .htaccess and RewriteRule

         

gsmforum

3:31 am on Mar 8, 2011 (gmt 0)

10+ Year Member



Hello,

I want to add this string to every URL on my server: "?language=en" (without quotes).

for instance: when someone goes to www.mypage.com/page.php it should rewrite automagically the URL to www.mypage.com/page.php?language=en

I was doing fine with this .htaccess configuration, but suddentely stopped working:


RewriteEngine On
RewriteRule ^$ ?language=en [QSA,L]



Can someone help ?
Thanks !

g1smd

4:37 pm on Mar 8, 2011 (gmt 0)

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



First off, are you using a package like Joomla, Zencart, Wordpress, or similar?

gsmforum

4:50 pm on Mar 8, 2011 (gmt 0)

10+ Year Member



No, running just a vbulletin board, as that string will convert all posts into English language automatically.

g1smd

4:57 pm on Mar 8, 2011 (gmt 0)

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



Your pattern ^$ can never match a request for example.com/page.php?<something>.

You would need
RewriteRule ^page\.php$ /page.php?lang=en [QSA,L]

gsmforum

7:41 pm on Mar 8, 2011 (gmt 0)

10+ Year Member



Thank you, strangely it was working with the other pattern.

I would need a pattern like "*", because i have php files, html files and also directories (like www.myboard.com/my-forum-directory/section1 section2, etc)

Isn't there a string capable of doing it, add it to ALL URLs ?

g1smd

7:51 pm on Mar 8, 2011 (gmt 0)

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



Yes, but by adding it to ALL URLs you'll add it to the URLs for all your images, your stylesheets, even to your robots.txt file.

You might want to reconsider what you actually need.

jdMorgan

7:17 pm on Mar 9, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Stop your code from looping and use a more-specific pattern, such as:

RewriteEngine on
#
# Rewrite to append "language=en" query string to .html, .htm, .php, or index
# files at any directory level, unless that query string is already present
RewriteCond %{QUERY_STRING} !([^&]+&)*language=en(&.*)?$
RewriteRule ^(([^/]/)*[^.]+\.(html?|php))?$ /$1?language=en [QSA,L]

Jim

gsmforum

11:07 pm on Mar 9, 2011 (gmt 0)

10+ Year Member



Thank you Jim!
will try that!

Any way to add that code to exclude one directory only, such as website/dir/?

jdMorgan

1:29 am on Mar 10, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Adding

RewriteCond $1 !^dir/

at the top would likely do nicely...

Jim