简体   繁体   中英

htaccess Redirect all pages except some not working

I'm trying to redirect all pages on my website to the homepage, except a few. I found some solutions on stackoverflow but none of these are working so I assume there might be conflicts with the rest of my htaccess which I didn't write by myself.

RewriteEngine on
# begin my code
RewriteCond %{REQUEST_URI} !^/agb$
RewriteCond %{REQUEST_URI} !^/impressum$
RewriteCond %{REQUEST_URI} !^/datenschutzerklaerung$
RewriteRule .* https://www.mywebsite.com [R=301,L]
# end my code
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]

I have tried to write RewriteRule .* / [R=301,L]

and I've also tried to put my code beneath the rest, but this results in a redirect loop.

Try it like this:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(agb|impressum|datenschutzerklaerung|)$
RewriteRule .* https://www.mywebsite.com [R=301,L]

Try in a new browser in incognito mode.

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