Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 删除上次文件夹访问后的所有内容_Regex_Apache_.htaccess_Mod Rewrite_Url Rewriting - Fatal编程技术网

Regex 删除上次文件夹访问后的所有内容

Regex 删除上次文件夹访问后的所有内容,regex,apache,.htaccess,mod-rewrite,url-rewriting,Regex,Apache,.htaccess,Mod Rewrite,Url Rewriting,我有以下htaccess代码: RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([^/\.]+)/([^/\.]+) index.php?task=location&state=$1&city=$2 [L] 它的URL类似于: website.com/texas/houston website.com/new-york/new-york ... 如果我访

我有以下htaccess代码:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/([^/\.]+) index.php?task=location&state=$1&city=$2 [L]
它的URL类似于:

website.com/texas/houston
website.com/new-york/new-york
...
如果我访问网址website.com/texas/houston/bfdsjfds,我会得到与网址website.com/texas/houston相同的内容。
如何删除上面示例中的“/bfdsjfds”?或者将该页面重定向到404。

您可以使用端锚
$
仅匹配
/state/city

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/([^/.]+)/?$ index.php?task=location&state=$1&city=$2 [L]

对于
website.com/texas/houston/bfdsjfds
类型的URL,这将自动返回404。

已经尝试过,但在此代码中不起作用。我已经成功地将其用于其他重写规则,如:RewriteRule^contact?$index.php?task=contact[L]。但在我的问题代码中,它不起作用。你能再澄清一下你的评论吗?对于哪个URL它不起作用?当它不工作时,你会遇到什么问题?你有更多的规则吗?如果有,那就把它们贴在问题上。我刚刚意识到我有多傻:)你的解决方案确实有效!在我的开始问题中,我有其他的重写规则。我必须在所有这些文件中添加“$”才能使其生效。当然,这是很明显的。谢谢!