如何使 if else 条件在 .htaccess 中重定向
How to make a if else conditions to redirect in .htaccess
基本上我不能return一个css, js, image or fonts with php 作为一个请求的结果,因为它被浏览器读取为一个html 文件,因此,我正在尝试在 .htaccess 中执行 if else 条件以重定向请求,因此我不必处理 php.
中的那些文件
此外,无论如何我都需要强制使用 https。
我正在使用 Apache/2.2.31,所以我不能使用语法 <If "true"> </If>
.
可选的是处理请求头,这样浏览器就可以读取文件的真实类型,但我不知道如何。
我是 .htaccess 的新手,我很确定这应该很容易。
代码如下:
RewriteEngine On
#this is just to forcing https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
# if css, js, html, images and fonts, do not redirect anything
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png|woff?2|otf|html)$ [NC]
RewriteRule # I don't know how to do anything
# else if the folder is “admin”, don’t do anything
RewriteCond %{THE_REQUEST} /admin/([^\s?]*) [NC]
RewriteRule # I don't know how to do anything
# else if the file is php or a folder, redirect to public folder
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ public/ [L,NC]
如有任何帮助,我们将不胜感激。
我猜这就是你想要的非常接近
RewriteEngine On
# this is just to forcing https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# IF the folder is “admin”, don’t do anything
RewriteCond %{REQUEST_URI} /admin/(.*) [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]
# END IF
# IF the file is PHP or a folder, redirect to the public folder
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_URI} ^(.*)\.php$ [NC]
RewriteRule ^(.*)$ "https://%{HTTP_HOST}/public/" [R=302,L]
# ELSE if file is css, js, html, images and fonts, do not redirect anything
# Do nothing
# END IF
重定向结果:
http://example.com/admin/index.php --> https://example.com/admin/index.php
http://example.com/index.php --> https://example.com/public/index.php
http://example.com/main.js --> https://example.com/main.js
演示:https://htaccess.madewithlove.be?share=9a712335-897f-57d3-9127-3bc5d747c0a7
基本上我不能return一个css, js, image or fonts with php 作为一个请求的结果,因为它被浏览器读取为一个html 文件,因此,我正在尝试在 .htaccess 中执行 if else 条件以重定向请求,因此我不必处理 php.
中的那些文件此外,无论如何我都需要强制使用 https。
我正在使用 Apache/2.2.31,所以我不能使用语法 <If "true"> </If>
.
可选的是处理请求头,这样浏览器就可以读取文件的真实类型,但我不知道如何。
我是 .htaccess 的新手,我很确定这应该很容易。
代码如下:
RewriteEngine On
#this is just to forcing https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
# if css, js, html, images and fonts, do not redirect anything
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png|woff?2|otf|html)$ [NC]
RewriteRule # I don't know how to do anything
# else if the folder is “admin”, don’t do anything
RewriteCond %{THE_REQUEST} /admin/([^\s?]*) [NC]
RewriteRule # I don't know how to do anything
# else if the file is php or a folder, redirect to public folder
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ public/ [L,NC]
如有任何帮助,我们将不胜感激。
我猜这就是你想要的非常接近
RewriteEngine On
# this is just to forcing https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# IF the folder is “admin”, don’t do anything
RewriteCond %{REQUEST_URI} /admin/(.*) [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]
# END IF
# IF the file is PHP or a folder, redirect to the public folder
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_URI} ^(.*)\.php$ [NC]
RewriteRule ^(.*)$ "https://%{HTTP_HOST}/public/" [R=302,L]
# ELSE if file is css, js, html, images and fonts, do not redirect anything
# Do nothing
# END IF
重定向结果:
http://example.com/admin/index.php --> https://example.com/admin/index.php
http://example.com/index.php --> https://example.com/public/index.php
http://example.com/main.js --> https://example.com/main.js
演示:https://htaccess.madewithlove.be?share=9a712335-897f-57d3-9127-3bc5d747c0a7