【问题标题】:Apache mod_rewrite: return a specific file if no file/directory existsApache mod_rewrite:如果不存在文件/目录,则返回特定文件
【发布时间】:2013-09-18 19:32:25
【问题描述】:

我正在编写一个使用 JavaScript 从数据库加载页面内容的 CMS。但我不想在我的网址中使用哈希 #

当请求路径/cms/post/123 时,Apache 应该加载文件/cms/index.html。但是如果路径下已经有一个文件,例如/cms/image.png,Apache 应该返回这个文件。另一点是,例如当请求/cms/admin/cms/admin/post/123 时,应该返回文件/cms/admin.html。 CMS的目录可能不同。

【问题讨论】:

  • 感谢您的详细要求,您具体尝试了什么?

标签: apache .htaccess mod-rewrite config


【解决方案1】:

您可以尝试在 /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]

您可以将它放在 CMS 所在的任何目录中。

【讨论】:

  • 看起来不错,我明天试试。感谢您的快速答复!
猜你喜欢
  • 2017-03-15
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 2011-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-03
相关资源
最近更新 更多