我需要将 .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>
当我想访问 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”结尾的内容重定向到没有它的相同地址(它从 URL 中删除 .php)。第二个条件和规则组合将请求的资源重新映射回以加载 php 文件(但地址将保持剥离状态)。
关于apache - 如何在htaccess问题中重写URL.php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65104087/