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
Apache 如何在htaccess问题中重写URL.php_Apache_.htaccess - Fatal编程技术网

Apache 如何在htaccess问题中重写URL.php

Apache 如何在htaccess问题中重写URL.php,apache,.htaccess,Apache,.htaccess,如果没有它,我需要将.htaccess中的每个文件从.php重写为一个干净的URL,但我无法让它工作 我当前的代码如下: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.php [NC,L] RewriteCond %{THE_REQUEST} !/(blog)\.php RewriteCond %{THE_REQUEST} \.phP RewriteRule ^(.*)\.php$

如果没有它,我需要将.htaccess中的每个文件从
.php
重写为一个干净的URL,但我无法让它工作

我当前的代码如下:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteCond %{THE_REQUEST} !/(blog)\.php
RewriteCond %{THE_REQUEST} \.phP
RewriteRule ^(.*)\.php$ /$1 [R=301,L]

RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteEngine On    
RewriteCond %{HTTP_HOST} ^url.info [NC] 
RewriteRule ^(.*)$ https://www.url.info/$1 [L,R=301]

Redirect 301 /url1.php /url2.php

ErrorDocument 404 /404.php

<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 6 months"
</IfModule>

# Deflate Compression by FileType
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/atom_xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-shockwave-flash
</IfModule>

# Deflate Compression by MimeType</span>
<IfModule mod_deflate.c>
<FilesMatch ".(js|jpg|jpeg|gif|png|css|txt|html)$">
ExpiresActive on
ExpiresDefault "access plus 1 month"
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>
重新编写引擎打开
重写cond%{REQUEST_FILENAME}-F
重写规则^([^\.]+)$$1.php[NC,L]
重写cond%{THE_REQUEST}/(博客)\.php
重写cond%{THE_REQUEST}\.phP
重写规则^(.*)\.php$/$1[R=301,L]
重新启动发动机
重写cond%{HTTPS}=在…上
重写规则。*https://%{HTTP_HOST}%{REQUEST_URI}[R=301,L]
重新启动发动机
RewriteCond%{HTTP_HOST}^url.info[NC]
重写规则^(.*)$https://www.url.info/$1[L,R=301]
重定向301/url1.php/url2.php
ErrorDocument 404/404.php
过期于
ExpiresDefault“访问加6个月”
#按文件类型缩减压缩
AddOutputFilterByType泄气文本/普通
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE应用程序/xml
AddOutputFilterByType DEFLATE应用程序/xhtml+xml
AddOutputFilterByType DEFLATE应用程序/rss+xml
AddOutputFilterByType DEFLATE应用程序/atom\u xml
AddOutputFilterByType DEFLATE应用程序/javascript
AddOutputFilterByType DEFLATE应用程序/x-javascript
AddOutputFilterByType放气应用程序/x-shockwave-flash
#通过MimeType对压缩进行放气
过期于
ExpiresDefault“访问加1个月”
SetOutputFilter放气
当我想访问URL1时,它会给出一个404。它可以自己工作,也可以与其他任何东西一起工作,但当我在开始添加php部分时就不行了。
我希望您能帮助我,谢谢。

以下应该可以:

RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,L]

第一个重写条件和规则组合将以“.php”结尾的所有内容重定向到没有“.php”的同一地址(它从URL中剥离了.php)。第二个条件和规则组合将重新映射请求的资源以加载php文件(但地址仍将被剥离)。

最好使用
apache
标记您的资源,因为
.htaccess
是apache web服务器的一项功能,而不是php。
RewriteCond%{The_REQUEST}\.phP
-大写的
P
将导致此条件始终失败。它没有。。。我写了完整的htaccess代码,我希望你能看到一个错误,因为我无法找出它。。。谢谢你的帮助这对我很有用。。。我确实在Windows中使用XAMPP进行了测试。也在FreeBSD托管服务器上进行测试-工作。从你的整个代码中考虑的事情很少:1。重定向到HTTPS应该在最开始的时候。2.无需多次启动发动机。3.重定向到www应该是HTTPS之后的第二个。你最终可以把它们合并成一条规则。@Damysus当你说“它没有”时,它做了什么吗?是否替换了当前
.htaccess
文件中的相应指令?您添加到问题中的
.htaccess
不正确。您正在请求/测试哪些URL?您好,是的。我确实写了发送到这里的代码。它一直工作,直到我尝试将301从url1重定向到url2。我尝试删除整个代码并一点一点地添加,它在PHP PAR中失败(404),当您访问没有php和200 OK的URL时,.php扩展。