I am having difficulty configuring a mod_rewrite rule in my .htaccess file to hide certain subdirectories.
Input: https://example.com/hidden_sub1/hidden_sub2/additional_sub/file.php?lang=en-US
Desired output: https://example.com/en-US/additional_sub/file
I had code that allowed me to hide something if I specifically name it:
RewriteEngine On RewriteBase / RewriteCond %{THE_REQUEST} ^GET\ /hidden_sub1/hidden_sub2/ [NC] RewriteRule ^hidden_sub1/hidden_sub2/(.*) /$1 [L,R=301] RewriteRule !^hidden_sub1/hidden_sub2/ hidden_sub1/hidden_sub2%{REQUEST_URI} [L]
However, it stopped working once I added in the following language subdirectory code:
RewriteRule ^([a-z]{2}(-[A-Z]{2})?)/(.*) $3?lang=$1 [L,QSA]
Besides, the name of the subdirectory is dynamic. "hidden_sub1" might be "random_name243". However, they would always be the first two subdirectories.
So how do I take out the first two randomly-named subdirectories while still keeping the language subdirectory?
I appreciate any help.
1 Answers
Answers 1
To change your url from the form
To the form :
You can use the following Rewrite rule
RewriteEngine On RewriteBase / #1)externally redirect "/hidden_sub1/hidden_sub2/foo.php?lang=bar" to "/bar/foo" RewriteCond %{THE_REQUEST} /hidden_sub1/hidden_sub2/([^.]+)\.php\?lang=(.+)\sHTTP [NC] RewriteRule ^ /%2/%1? [L,R=301] #2)internally rewrite "/bar/foo" to "/hidden_sub1/hidden_sub2/foo.php?lang=bar" RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^/]+)/([^/]+)/?$ /hidden_sub1/hidden_sub2/$2.php?lang=$1 [NC,L]
Tested on apache 2.2. and 2.4.
0 comments:
Post a Comment