.htaccess to load index file for url with variable number of directories?

自闭症网瘾萝莉.ら 提交于 2019-12-24 11:28:38

问题


lets say I have directory regions in my root:

root/regions

I want to load index.php file which is located in regions folder if user visits one of the following:

http://www.example.com/regions/united-kingdom
http://www.example.com/regions/united-kingdom/london
http://www.example.com/regions/united-kingdom/londom/rm8-16de

Country, city and post code dont exist as directory ! How to do that? Thank you in advance!


回答1:


In your regions/.htaccess you can have this .htaccess:

DirectoryIndex index.php
RewriteEngine On
RewriteBase /regions/

# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]



回答2:


The order should be DirectoryIndex index.html index.php // default is index.html.

If the webserver finds no files in the current directory that matches names in the DirectoryIndex directive, then a directory listing will be displayed to the browser, showing all files in the current directory. Apache will look for each of the above files, in order, and serve the first one it finds when a visitor requests just a directory.

Once check DirectoryIndex which is solution for you.

Let me know if you face any query/concern regarding this.

Thanks!



来源:https://stackoverflow.com/questions/31532365/htaccess-to-load-index-file-for-url-with-variable-number-of-directories

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