简体   繁体   中英

htacess redirect all pages to home except admin directory

I have the following code my htaccess file:

RewriteCond %{REQUEST_URI} !^/admin/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/admin
RewriteCond %{REQUEST_URI} !\.(?:jpe?g|gif|bmp|png|tiff|css|js)$ [NC]
RewriteRule ^(.*)$ /index.php [L]

I know coding but not good with regex and I didn't play with htaccess file much before. I found this answer and also this one but however they didn't meet my requirements.

I am using the directory names as permlinks. For example, if user enters www.mydomaincom/about a JavaScript code extracts the directory name, compares with the database and polls a page. It also works when the address is written with a trailing slash like www.mydomaincom/about/ . But there is a problem with this notation.

The wierd thing is, the page is trying to read the html data from index.php which is AOK. But... The elements which need sources like <img>, <style>, <script> or style properties which need source directories, are trying to seek for sources from about/ directory as if it does exist.

I am searching for a htaccess solution which will make www.mydomaincom/about/ notation behave the same as www.mydomaincom/about without affecting the source attributes or properties.

You have too many lines in your code:

RewriteCond %{REQUEST_URI} !^/admin/
RewriteCond %{REQUEST_URI} !/admin

These seeingly do the same thing, but they dont.
The first one does "If it does not start ( ^ ) with /admin " The second does "if it is literally not /admin "

Both are not what you want:

RewriteCond %{REQUEST_URI} !^/admin

This does "if it does not start with /admin ". This also includes /admin/ , as that starts with /admin


Second weird combo is this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(?:jpe?g|gif|bmp|png|tiff|css|js)$ [NC]

The first one is "if it is not a file"
The second one you do "if it is not a css,js,png,etc"
That last line isn't needed, as the filetypes are files, so you check the same thing twice.


Third, you might want to pass the url to your index.php. Shortend and passing it to the index.php looks like this:

RewriteCond %{REQUEST_URI} !^/admin
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?url=$1 [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