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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog