将基域重写为index.php

时间:2017-07-08 12:22:55

标签: .htaccess redirect mod-rewrite indexing url-rewriting

我试图在我的PHP项目上创建一个动态系统,这就是我的.htaccess的样子:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteRule ^pages/?$ resources/pages/page.php [NC,L]
    RewriteRule ^([A-Za-z0-9-]+)?$ resources/pages/post-or-category.php?tcs=$1 [NC,L]   
</IfModule>

/ pages /和动态帖子或类别页面有效,但如果上述方法没有重定向,则第二条规则会重定向所有内容。由于它是用于获取帖子和类别的PHP脚本,因此我不想对其进行索引检查。如果域中没有任何内容(例如:www.domain.comdomain.com),是否有办法重写。

此外,我将脚本中的帖子和类别与URL上的最后一个世界区分开来(例如:帖子为P435345,类别为C935943 [数字为id&#39; s])。是否可以检查网址的最后一个单词是否以.htaccess中的P或C开头,以将其他网址重定向到404?

1 个答案:

答案 0 :(得分:0)

RewriteCond之前使用指令RewriteRule,因为如果存在任何文件或目录,则不会重定向。例如。它不会将domain.com/index.php重定向到resources/pages/post-or-category.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d