简体   繁体   中英

.htaccess redirect but exclude some folder

Hello there I am using cpanel. In the cpanel, I created a redirect, which created an .htaccess like this.

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteOptions inherit
Options -Indexes

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php73” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php73 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
RewriteCond %{HTTP_HOST} ^paw\.xevz\.migor\.eu$ [OR]
RewriteCond %{HTTP_HOST} ^www\.paw\.xevz\.migor\.eu$
RewriteRule ^/?$ "http\:\/\/paw\.xevz\.migor\.eu\/mainfolder" [R=301,L]

The problem with this, when I added new folder my public directory and view my page, it will always redirect to the 'mainfolder'. How will I add a RewriteCond that will not redirect to the main folder if my link is something like http://paw.xevz.migor.eu/subfolder ? I tried like this,

RewriteCond %{REQUEST_URI} !^/subfolder/    
RewriteCond %{HTTP_HOST} ^paw\.xevz\.migor\.eu$ [OR]
RewriteCond %{HTTP_HOST} ^www\.paw\.xevz\.migor\.eu$
RewriteRule ^/?$ "http\:\/\/paw\.xevz\.migor\.eu\/mainfolder" [R=301,L]

but no luck. Please help thank you.

PS Those links in my .htaccess are just sample.

RewriteRule ^/?$ will only match and redirect landing page URI. You may have a front controller rule in the end that rewrites everything to something like ?page=something and that would trigger this redirect rule for all the URIs that are not for a file or directory.

You may use this redirect rule instead:

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(?:www\.)?paw\.xevz\.migor\.eu$ [NC]
RewriteRule ^$ /mainfolder/ [R=302,L]

Make sure this is your topmost rules below RewriteEngine On line and you test it after clearing your browser cache.

Once you verify it is working fine, replace R=302 to R=301 . Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

RewriteCond %{ENV:REDIRECT_STATUS} ^$ will make sure this rule is not executed after 1 or more rewrites due to other rules.

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