Solved

URL Rewrite Solution

Posted on 2010-09-03
5
261 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Mysql is corrupting stringified JSON object 11 53
Apache HTTP to HTTPS redirect please. 5 45
PHPmailer and exchange online(Office365) 3 293
PHP in Apache server 20 88
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…
It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

911 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

23 Experts available now in Live!

Get 1:1 Help Now