简体   繁体   中英

URL Rewrite based on trailing slashes in requested url

I'm working on a dynamic php website which serves content from database. Currently I'm having four PHP files- index.php , a.php , b.PHP , c.php . These serves result from the database as par the requested URL. eg.

  1. If the requested url example.com then index.php serves results.

  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 . [rewrite example.com/a.php to 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 ].

  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 ).

In short if the requested url is -

example.com or example.com/ - then results will be served by index.php

example.com/anything.html - then result will be served by a.php

example.com/anything/anything.html - then result will be served by b.php

example.com/anything/anything/anything.html - then result will be parsed by c.php

So how can i achieve such functionality using URL rewritting ? Please reply with .htaccess code for such functionality.

Put this code in your DOCUMENT_ROOT/.htaccess file:

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]

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