Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.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
.htaccess 如何在mod_rewrite,htaccess中解决这个问题?_.htaccess_Mod Rewrite - Fatal编程技术网

.htaccess 如何在mod_rewrite,htaccess中解决这个问题?

.htaccess 如何在mod_rewrite,htaccess中解决这个问题?,.htaccess,mod-rewrite,.htaccess,Mod Rewrite,我在使用.htaccess删除php扩展时遇到问题。规则如下: RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(.*)$ $1.php?$2 [QSA,L] 这个链接sak.ps/overview在www.sak.ps/overview.php中运行良好,但是sak.ps/overview/1在sak.ps/overview.ph

我在使用.htaccess删除php扩展时遇到问题。规则如下:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php?$2 [QSA,L]
这个链接sak.ps/overview在www.sak.ps/overview.php中运行良好,但是sak.ps/overview/1在sak.ps/overview.php中?flag=1给出了内部服务器错误

但在这两种情况下,URL都显示在带有php扩展名的URL选项卡中。

您不需要这个吗?$2在您的规则末尾,QSA标志将根据其定义自动附加查询字符串

如果我没有弄错的话,错误是因为您的规则正则表达式中没有第二个捕获组。

1。在您的规则中,$2将始终为空,因为您没有相应的捕获组

二,。您的规则仅适用于将.php扩展名添加到请求URL末尾的URL。对于第二个URL示例,您需要有单独的规则

RewriteEngine on

# 1) add .php file extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]

# 2) more complex case of adding .php extension
# will work with URLs like
# /overview/something/here => /overview/something.php?flag=here
# /overview/1 => /overview.php?flag=1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+)/([^/]+)$ $1.php?flag=$2 [QSA,L]

# 3) Another type of URL
# will work with URLs like
# /overview/something/here => /overview.php?flag=something/here
# /overview/1 => /overview.php?flag=1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^([^/]+)/(.+)$ $1.php?flag=$2 [QSA,L]
我提供了两种方法的规则:

规则2将处理/overview/something/here=>/overview/something.php?flag=此处最后一个段将被视为参数,而之前的所有段将被视为文件名

规则3将处理/overview/something/here=>/overview.php?flag=something/这里第一个段将被视为文件名,而所有其他段将被视为参数

/overview/1=>/overview.php?flag=1重写可以由它们2或3处理