dongyou7472 2015-10-02 19:57
浏览 73
已采纳

如何使用.htaccess重写特定网址条件和常规网址条件的网址?

I am working on a website with a custom (basic) CMS system using PHP. The CMS system lives under admin.php and the rest of the site lives under index.php.

// Project explanation for more context on the problem (skip to next comment for the actual problem) //

Both index.php and admin.php takes in GET variables to control the contents of the page. admin.php takes in 3 different GET variables (a, b and c) which defines different funcitonalities.

"A" is for main functionality like "pages - contents of the different pages", "News - Basic blog system" etc.

"B" is sub links specifically for the "pages" functionality to define which page the admin user is modifying.

"C" is also specific for the "pages" functionality and defines the sub-page that the admin user is modifying.

Then the index.php which is the "client-side" of the project takes in two get variables (A and B)

"A" being the page the user is visiting, defined in the database.

"B" being the sub-page the user is visiting, -//-.

// End of project explenation //

So I have been trying to use Rewrite in htaccess to rewrite the URL to a more user-friendly string.

(admin.php) Example: admin.php?a=pages&b=home => /admin/pages/home

(index.php) Example: index.php?a=about&b=team => /about/team

And so far the admin Rewrite conditions work perfectly. But I want to implement the client Rewrite conditions, and they collide.

Current .htaccess state:

RewriteEngine On

RewriteRule ^admin/? admin.php [NC,L]
RewriteRule ^admin/([A-Za-z0-9-&]+)/?$ admin.php?a=$1 [NC,L]
RewriteRule ^admin/([A-Za-z0-9-&]+)/?/([A-Za-z0-9-&]+)/?$ admin.php?a=$1&b=$2 [NC,L]
RewriteRule ^admin/([A-Za-z0-9-&]+)/?/([A-Za-z0-9-&]+)/?/([A-Za-z0-9-&]+)/?$ admin.php?a=$1&b=$2&c=$3 [NC,L]

I have been looking around on the htaccess documentation and on several questions on stack but havn't been able to find a solid solution.

How can I rewrite the url based on if the user is visiting the admin page or the main (index) page?

Thanks!

  • 写回答

1条回答 默认 最新

  • doudou201701 2015-10-02 21:14
    关注

    You can use that:

    RewriteEngine On
    
    # skip all files and directories from rewrite rules below
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]
    
    # rewrite admin/
    RewriteRule ^admin/?$ admin.php [QSA,NC,L]
    RewriteRule ^admin/([^/]+)/?$ admin.php?a=$1 [QSA,NC,L]
    RewriteRule ^admin/([^/]+)/([^/]+)/?$ admin.php?a=$1&b=$2 [QSA,NC,L]
    RewriteRule ^admin/([^/]+)/([^/]+)/([^/]+)/?$ admin.php?a=$1&b=$2&c=$3 [QSA,NC,L]
    
    # rewrite others
    RewriteRule ^([^/]+)/?$ index.php?a=$1 [QSA,L]
    RewriteRule ^([^/]+)/([^/]+)/?$ index.php?a=$1&b=$2 [QSA,L]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算