[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

URL Rewrite Solution

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
urwa2010
Asked:
urwa2010
1 Solution
 
georgekrajCommented:
0
 
urwa2010Author Commented:
No. Not any helpful. Need a solution.
0
 
arober11Commented:
Just add a QSA (Query String Append) flag to you rule e.g.

RewriteRule ^en/(.*)$ http://www.domain.com/$1 [QSA,L]
0
 
HackneyCabCommented:
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
 
urwa2010Author Commented:
HackneyCab You are my hero. That's how an expert understands the problem and help.s Awesome. Will try it out.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now