简体   繁体   English

Apache mod_rewrite:如果不存在文件/目录,则返回特定文件

[英]Apache mod_rewrite: return a specific file if no file/directory exists

I'm writing a CMS that uses JavaScript to load the content of a page from the database. 我正在编写一个使用JavaScript从数据库加载页面内容的CMS。 But I don't want hashes # in my URLs. 但是我不想在我的URL中使用哈希#

When a path is requested /cms/post/123 , Apache should load the file /cms/index.html . 当请求路径/cms/post/123 ,Apache应该加载文件/cms/index.html But if there's already a file under the path, for example /cms/image.png , Apache should return the file. 但是,如果路径下已经有一个文件,例如/cms/image.png应该返回该文件。 Another point is that when for example /cms/admin or /cms/admin/post/123 is requested the file /cms/admin.html should be returned. 另一点是,例如,当请求/cms/admin/cms/admin/post/123应返回文件/cms/admin.html The directory of the CMS may be different. CMS的目录可能不同。

You can try using these rules in an htaccess file in your /cms/ directory 您可以尝试在/cms/目录的htaccess文件中使用这些规则

Options -Multiviews
RewriteEngine On

# if file exists, serve the file
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]

# if request is for admin, load admin.html
RewriteRule ^admin/ admin.html [L]

# everything else gets routed to index.html
RewriteRule ^ index.html [L]

You can place this in any directory that the CMS is in. 您可以将其放置在CMS所在的任何目录中。

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

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