是否可以只允许正确的 pretty url?

Is it possible to allow only the correct pretty url?

我有这些漂亮的 urls:

.htaccess

RewriteEngine On
RewriteRule ^([a-z]+)/([a-z]+)/?([0-9]*)$ index.phpcontroller=&action=&id= [NC,L]

是否可以定义任何其他 url 回到索引?

p.e. app/i/am/the/fastest/man/alive ---> 应用/

不胜感激!

你可以为此制定一个新规则:

RewriteEngine On
RewriteBase /app/

RewriteRule ^([a-z]+)/([a-z]+)/?([0-9]*)$ index.php?controller=&action=&id= [NC,L,QSA]

# 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]

我添加了?在 index.php

之后

你问题的答案

Is it possible to allow only the correct pretty url?

有点像,但您必须更具体地制定规则以限制它们。举个例子。

RewriteEngine On
RewriteBase /app

#rewrite rule if starts with /app/product or /app/category
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(product|category)/([a-z]+)/([0-9]*)/?$ index.php?controller=&action=&id= [NC,L]
#else redirect to homepage
RewriteRule . app/ [R=301,L]

这将确保只有 URL 类似 http://example.com/app/product/1 的内容会被重写,而所有其他类似 http://example.com/app/some/other/page 的内容将被重写到您的主页。