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

标签 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$ /$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/

相关文章:

apache - Mod_rewrite 已启用但不起作用

apache - Apache 的 RewriteRule 的 $1 替换不会返回第一个匹配的表达式

php - 如何在 .htaccess 中禁用 mod_security 和 mod_security2

.htaccess - 通过 htaccess 在重写的 url 上强制使用 HTTPS

android - 将 Apache HttpClient 与 Android SDK 23 结合使用。 NoSuchMethod

php - Cockpit CMS 安装后不会由 Apache 提供服务(404 错误)

python - 管理多个 settings.py 文件

linux - 如何在 Linux CentOS 中为除 root 以外的普通用户启动/停止 Tomcat

apache - 规范 URL 和 URL 重写

PHP 获取 URL 作为变量