Solved

URL Rewrite Solution

Posted on 2010-09-03
5
250 Views
Last Modified: 2012-08-13
Hi,

I have URLs of the following type:

http://www.domain.com/en/
http://www.domain.com/en/something1.php
http://www.domain.com/en/something.php?lang=en&var=val
http://www.domain.com/en/something1.php?
http://www.domain.com/en/something1.php?var2=val

I want any URL that starts with /en/ to be redirected to /

i.e http://www.domain.com/en/something1.php  to  http://www.domain.com/something1.php

and if the query string is present it should be attached as well. This is the easy part and is working.
RewriteRule ^en/(.*)$ http://www.domain.com/$1 [L]

The problem for which I need expert help is with attaching lang=val to query string in various cases like when no query string is present, when it's not present when query string already has lang etc.

I need a smart solution ASAP.
0
Comment
Question by:urwa2010
5 Comments
 
LVL 6

Expert Comment

by:georgekraj
ID: 33595386
0
 

Author Comment

by:urwa2010
ID: 33595882
No. Not any helpful. Need a solution.
0
 
LVL 26

Expert Comment

by:arober11
ID: 33601485
Just add a QSA (Query String Append) flag to you rule e.g.

RewriteRule ^en/(.*)$ http://www.domain.com/$1 [QSA,L]
0
 
LVL 16

Accepted Solution

by:
HackneyCab earned 500 total points
ID: 33614669
This is actually two directives and they can be handled separately.

You've already worked out the simple redirect from /en to /, so that's in the bag.

After that, you just need to use RewriteCond directives to check for various states of query-string. You can check the query-string value and act on it using this syntax:

# Query string empty, so add default lang value
RewriteCond %{QUERY_STRING} ^$
RewriteRule .* $1?lang=default-lang-value

# Query string non-empty, but does not currently contain
# valid lang value, so append default lang value to query string
RewriteCond %{QUERY_STRING} !lang=[a-zA-Z]+
RewriteRule .* $1?lang=default-lang-value [QSA]

You need to modify these to suit you, and add any extra sets depending on how you want to handle other query string possibilities. Note that the [QSA] flag tells RewriteRule to add to the existing query string rather than overwrite it.

Also, the [L] flag tells Apache not to check for other matching patters during this pass. It does NOT stop Apache from referring to the .htaccess file once the new request has been calculated. Using [L] without having a reason is probably harmful to performance. In your case, you probably want Apache to deduce the non-en URL and then, in the same pass, modify the query string if necessary. Having the [L] flag forces Apache to do two passes.
0
 

Author Comment

by:urwa2010
ID: 33617224
HackneyCab You are my hero. That's how an expert understands the problem and help.s Awesome. Will try it out.
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

Over the last year I have answered a couple of basic URL rewriting questions several times so I thought I might as well have a stab at: explaining the basics, providing a few useful links and consolidating some of the most common queries into a sing…
If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now