Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.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
ApacheModu重写SEO URL,但如果文件不';不存在_Apache_Mod Rewrite - Fatal编程技术网

ApacheModu重写SEO URL,但如果文件不';不存在

ApacheModu重写SEO URL,但如果文件不';不存在,apache,mod-rewrite,Apache,Mod Rewrite,我找不到这个问题的答案,所以请让我知道之前是否解决了这个问题 我使用mod_rewrite来创建“漂亮”的URL,但是如果您请求一个不存在的文件(比如输入错误),它会多次重定向并添加.php,然后失败。我的代码如下: RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !(.*)/$ RewriteRule ^(.*)$ http://inquisito.rs/

我找不到这个问题的答案,所以请让我知道之前是否解决了这个问题

我使用mod_rewrite来创建“漂亮”的URL,但是如果您请求一个不存在的文件(比如输入错误),它会多次重定向并添加.php,然后失败。我的代码如下:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://inquisito.rs/$1/ [R=301,l]
RewriteRule ^(.*)/$ /$1.php [L]
因此,如果你转到它,它会显示aion页面,但是如果你转到,比如,inquisito.rs/aio/on chance,它会给出这个


提前感谢您,我无法告诉您有多少次我使用这里的信息来解决工作和家庭中的问题。

使用您给出的示例,以下是规则的应用方式:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f        # /aio/ is not a file, so this matched
RewriteCond %{REQUEST_URI} !(.*)/$         # This DOES NOT match, because you have a trailing slash
RewriteRule ^(.*)$ http://inquisito.rs/$1/ [R=301,L]  # This rule doesn't run, because the condition above wasn't met

# This rule is separate from the RewriteConds above
RewriteRule ^(.*)/$ /$1.php [L]            # This does match because of the lack of RewriteConds and because you have a trailing slash
请尝试以下(未测试的)规则集:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f      # Make sure no matching file exists
RewriteCond %{REQUEST_URI} !\.php$         # Don't match requests that already end .php
RewriteCond %{REQUEST_URI} !(.*)/$       # Check for missing trailing slash
RewriteRule ^(.*)$ http://inquisito.rs/$1/ [R=301,L]  # Redirect with trailing slash

# Separate rule
RewriteCond %{REQUEST_URI} !\.php$       # Don't match requests that already end .php
RewriteRule ^(.*)/$ /$1.php [L]          # Internal redirect to matching PHP file

需要注意的是,所有匹配的重写规则都会导致htaccess再次处理新请求。

这似乎不起作用,尽管解释很有用。我将仔细研究一下,看看是否无法修复。我将答案更新了几次,可能仍然不太正确,但值得检查您是否正在使用最新的更新。此最新版本似乎有效,但如果您尝试访问不存在的内容,它仍然会出错,并给出相同的重定向循环。我认为有一种方法可以测试文件是否存在,然后从中剥离.php,如果文件不存在,然后重写为404,不是吗?应该是类似于那里的东西,尽管我不太清楚它将如何工作,我只是用来帮助,并意识到在内部重定向发生后,第一组条件得到了满足(因为请求现在以.php结束,并且没有尾随斜杠!)。在我的答案中尝试新的规则。