Someone33
Someone33

Reputation: 568

rewrite get request htaccess

I have the following htaccess rule

Options All -Indexes 
RewriteEngine On
RewriteRule ^welcome/([a-zA-Z0-9_-]+)/?\.html$ welcome.php?lang=$1 [QSA,NC,L]

This works fine for the welcome.php page and the 3 lang=(de/en/it), but only if i'm typing in the URL in the browser address bar.

If i'm switching from the language form, the GET request will be appended to the rewritten URL like

 welcome/de.html?lang=de

How can i rewrite the GET requests so the URL will be

 welcome/en.html

and not

welcome/de.html?lang=en

Also, is it possible to apply a general rule for all pages and not only for the welcome site.

Thanks in advance for helping

EDIT

Now my htaccess file is

Options All -Indexes 
RewriteEngine On

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(welcome)[^?]*\?lang=([^\s&]+) [NC]

RewriteRule ^/%1/%2.html? [R=301,L]

RewriteRule ^welcome/([a-zA-Z0-9_-]+)/?\.html$ welcome.php?lang=$1 [NC]

RewriteRule ^welcome/([\w-]+)/?\.html$ welcome.php?lang=$1 [QSA,NC,L]

But it's still not working. The GET query string is still appended like

  welcome/de.html?lang=de

Upvotes: 0

Views: 2253

Answers (2)

anubhava
anubhava

Reputation: 784998

You will need these 2 additional rules:

Options All -Indexes 
RewriteEngine On
RewriteBase /test/

RewriteCond %{THE_REQUEST} \s/+(test/[^/.?]+)[^?]*\?lang=([^\s&]+) [NC]
RewriteRule ^ %1/%2.html? [R=302,L]

RewriteRule ^welcome/([\w-]+)/?\.html$ welcome.php?lang=$1 [QSA,NC,L]

Upvotes: 1

Im0rtality
Im0rtality

Reputation: 3533

Idea:

  1. Rewrite welcome/de.html?lang=en to welcome/en.html (make sure this rule does NOT have [L] flag)
  2. Rewrite welcome/en.html to welcome.php?lang=en

You only need one rule for #1:

Options All -Indexes
RewriteEngine On
RewriteRule ^welcome\/([a-zA-Z0-9_-]+)?\.html\?lang=([a-zA-Z0-9_-]+)?$ welcome/$2.html [QSA,NC]
RewriteRule ^welcome\/([a-zA-Z0-9_-]+)?\.html$ welcome.php?lang=$1 [QSA,NC,L]

Rule #1: http://regex101.com/r/rX2gG5

Rule #2: http://regex101.com/r/aD3mV2

Upvotes: 0

Related Questions