Solved

URL Rewrite Solution

Posted on 2010-09-03
5
286 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you are running a LAMP infrastructure, this little code snippet is very helpful if you are serving lots of HTML, JavaScript and CSS-related information. The mod_deflate module, which is part of the Apache 2.2 application, provides the DEFLATE…
Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

724 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