.htaccess: Rewrite rule issue, how to redirect all to admin panel

喜欢而已 提交于 2020-01-05 00:50:07

问题


I want to redirect my website to admin panel

Like

localhost/website/ => localhost/website/admin
localhost/website/login => localhost/website/admin
localhost/website/blog => localhost/website/admin

Here is my created rule

RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /website/admin/$1 [L]

But this is not working


回答1:


The following code will redirect everything to your admin page, keeping the attributes. This also applies to files who already exist !

RewriteEngine On
RewriteCond %{REQUEST_URI} !^website/admin
#RewriteCond %{REQUEST_FILENAME} !-f   #OPTIONAL: Remove '#' if you want existing files to display normally
#RewriteCond %{REQUEST_FILENAME} !-d   #OPTIONAL: Remove '#' if you want existing files to display normally
RewriteRule ^(.*)$ /website/admin/ [L,QSA]

Explanation:

turn on the rewrite engine:

RewriteEngine On

specific condition for rewriting: uri (the part after your domain name/localhost) cannot (! = inverse, NOT) start (^ = start of line) with website/admin = where you are rewriting to. This must be done to prevent looping.

RewriteCond %{REQUEST_URI} !^website/admin

Following 2 lines can be uncommented (remove the #) if you want that existing files are not rewritten

#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d

The rewrite himself:

  • ^ = start of line
  • () = a substring
  • .* = a number of arbitrary symbols, or no symbols
  • $ = end of line
  • /website/admin = the place to rewrite to
  • [L,QSA] = extra options:
  • L=last: stop executing further lines
  • QSA=Query String Append: keep arguments, e.g. ?page=contact e.g.

    RewriteRule ^(.*)$ /website/admin/ [L,QSA]

I hope this clarifies my solution.




回答2:


Try:

RewriteEngine On
RewriteCond $1 !^website/admin
RewriteRule ^(.*)$ /website/admin [L]



回答3:


You can try this with some updated as per your code:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}  ^/index\.php/component/quates   [NC]
RewriteRule .*   index.php/component/quotes   [R=301,L]

Or Read this link:

http://coolestguidesontheplanet.com/redirecting-a-web-folder-directory-to-another-in-htaccess/




回答4:


This should work:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d      
RewriteCond %{REQUEST_URI} !(website/admin)$ [NC]
RewriteRule ^(.*)$ website/admin [R=301,L]

Explanation:

First we make sure that the request is not for a file or directory that exists:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

Then we see if website/admin is exactly our requested URL (case-insensitive [NC]):

^ indicates beginning, and $ end of expression.

RewriteCond %{REQUEST_URI} !^(website/admin)$ [NC]

If all conditions are met we redirect to website/admin with HTTP status code 301 (last step [L]):

RewriteRule ^(.*)$ website/admin [R=301,L]



回答5:


Try this:

RewriteEngine On
RewriteRule ^website/(?!admin).*$ website/admin [L]
RewriteRule ^website/admin[^/]+.*$ website/admin [L]


来源:https://stackoverflow.com/questions/26114191/htaccess-rewrite-rule-issue-how-to-redirect-all-to-admin-panel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!