Solved

URL Rewrite Solution

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
follow the php mysql tuto and tells me what's wrong! 10 31
DNS Question 7 64
PHP Upload using Uploadify 4 95
HHTP and HTTPS redirect question 3 110
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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 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