简体   繁体   English

根据请求的URL中的斜杠重写URL

[英]URL Rewrite based on trailing slashes in requested url

I'm working on a dynamic php website which serves content from database. 我正在一个动态的php网站上,该网站提供数据库中的内容。 Currently I'm having four PHP files- index.php , a.php , b.PHP , c.php . 目前我有四个PHP文件- index.phpa.phpb.PHPc.php These serves result from the database as par the requested URL. 这些将来自数据库的结果用作请求的URL。 eg. 例如。

  1. If the requested url example.com then index.php serves results. 如果请求的网址为example.comindex.php提供结果。

  2. If the requested url is example.com/xyz.html then a.php first exact xyz from the url and then serves result for xyz . 如果所请求的网址是example.com/xyz.htmla.php首先从该网址中准确输入xyz ,然后将结果提供给xyz [rewrite example.com/a.php to example.com/xyz.html ] [将example.com/a.php重写为example.com/xyz.html ]

  3. If the requested url is example.com/x/y.html then b.php first extract x and y from the requested url and then serves result for y page [rewrite example.com/a.php/b.php to example.com/x/y.html ]. 如果请求的URL是example.com/x/y.htmlb.php首先从请求的URL中提取xy ,然后将结果提供给y页面[将example.com/a.php/b.php重写为example.com/x/y.html ]。

  4. If the requested url is example.com/x/y/z.html the c.php first extract x , y , z from the url and then serve result for z page (rewrite example.com/a.php/b.php/c.php to example.com/x/y/z.html ). 如果要求的网址是example.com/x/y/z.htmlc.php首先从网址中提取xyz ,然后将结果提供给z页面(重写example.com/a.php/b.php/c.phpexample.com/x/y/z.html )。

In short if the requested url is - 简而言之,如果请求的网址是-

example.com or example.com/ - then results will be served by index.php example.comexample.com/结果将由index.php

example.com/anything.html - then result will be served by a.php example.com/anything.html然后结果将由a.php

example.com/anything/anything.html - then result will be served by b.php example.com/anything/anything.html然后结果将由b.php

example.com/anything/anything/anything.html - then result will be parsed by c.php example.com/anything/anything/anything.html然后结果将由c.php解析

So how can i achieve such functionality using URL rewritting ? 那么如何使用URL重写实现此类功能? Please reply with .htaccess code for such functionality. 请使用.htaccess代码答复此功能。

Put this code in your DOCUMENT_ROOT/.htaccess file: 将此代码放在您的DOCUMENT_ROOT/.htaccess文件中:

DirectoryIndex index.php

RewriteEngine On

RewriteRule ^(?!(a|b|c)\.php$)[^/.]+\.php$ /a.php [L,NC]

RewriteRule ^[^/]+/[^/.]+\.php$ /b.php [L,NC]

RewriteRule ^[^/]+/[^/]+/[^/.]+\.php$ /c.php [L,NC]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM