user2168049
user2168049

Reputation: 33

Htaccess 301 redirect with dynamic URLs

I need to redirect my dynamic URLs with parameters to subfolder URLs. The source URL can vary in length/levels.

An example would be:

Source:

http://www.example.com/dir/?lang=en

Desired:

http://www.example.com/dir/en

and another would be:

Source:

http://www.example.com/dir/subdir/?lang=en

Desired:

http://www.example.com/dir/subdir/en

I'm using WordPress, so there is some existing code in my .htaccess, which the redirect in question, needs to work with. My existing .htaccess looks like this:

RewriteEngine On
RewriteBase /

RewriteRule ^index\.php$ - [L]

RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L] 
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

I tried searching the entire web, however, no solution/answer seems to fit this particular case. Any experts who can help?

Upvotes: 3

Views: 4777

Answers (1)

anubhava
anubhava

Reputation: 784958

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} !^lang=.+ [NC]
RewriteRule ^(.+?)/([^/]+)/?$ $1/?lang=$2 [L,QSA]

Upvotes: 1

Related Questions