简体   繁体   中英

httaccess redirect to new URL with conditions

Trying to create a htaccess file that redirects to new URL but with conditions based on the beginning value of the URI. For example:

if

example.com/piasjgasngan will redirect to http://example2.com/index.php?id=paiajgasngan

( the first letter of p in the uri will be the condition topass to the new URL )

if

example.com/x9q8wutjgj will redirect to URL example2.com/download_app.html

( the first letter of x in the uri will be the condition )

else all other URI will rediect to a different URL

example.com/89qwtjggjqa will redirect to example2.com/invalid_URI.html

I have tried numerous different conditions

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond ^p$ [NC]
RewriteRule .* http://example2.com\P\index.php=id={REQUEST_URI} [R=301,NC,OR]
RewriteCond ^x$ [NC]
RewriteRule .* http://example2.com\download_app.html [R=301,NC,OR]
RewriteRule .* http://example2.com\bad_URI.html [R=301,L]

all attempts send to example.com/download_app.html

Hope my example identifies the what I am trying to accomplish TY in advance

Could you please try following, written based on your shown samples. Please make sure you clear your browser cache before testing your URLs.

RewriteEngine ON
##First character is p OR x of URI condition.
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{REQUEST_URI} ^/(p|x)(.*)/?$ [NC]
RewriteRule ^(.)$ http://example2.com/index.php?id=%1%2 [L]

##Anything else uri condition.
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteRule ^(.*)$ http://example2.com/invalid_URI.html [L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM