【问题标题】:htaccess rewrite url redirect php file to folderhtaccess 重写 url 将 php 文件重定向到文件夹
【发布时间】:2013-10-08 21:10:27
【问题描述】:

我想将此 php url 重定向到该文件夹​​。

我一直使用以下设置。但是自从 apache 更新到 2.4 之后,它就不再工作了。

目标: example.com/about.php?sub=about 或 example.com/about.php?sub= 应在浏览器中显示为 example.com/about/

我使用以下设置。

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(example.com)(:80)? [NC]
RewriteRule ^(.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.example.com/$1/ [L,R=301] 

RewriteRule ^about/$ about.php$1 [L]
RewriteRule ^about/([A-Za-z0-9-]+)$ about.php?sub=$1 [L]

这就是我现在使用的...

【问题讨论】:

  • [A-Za-z0-9-]+可以写成\w+
  • @hjpotter92 不是这样; \w 包含下划线。
  • @theftprevention 啊!它不在lua 中,因此,我的建议。

标签: apache .htaccess url mod-rewrite redirect


【解决方案1】:

我在您的代码中没有看到任何将/about.php?sub= 变为/about 的规则。

试试这个代码:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(about)\.php(\?sub=[^\s&]*)?\s [NC]
RewriteRule ^ /%1? [R=301,L]

RewriteRule ^about/?$ about.php?sub=about [L,NC,QSA]
RewriteRule ^about/([a-z0-9-]+)/?$ about.php?sub=$1 [L,NC,QSA]

# Add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R]

【讨论】:

  • .htaccess 中必须有 -Multiviews 吗?只是好奇......但我仍然有同样的问题我只在浏览器中使用 sub= 或 sub=about 获取文本,子页面是一个仅包含文本的单个 php 文件。通过链接 www.example.com/about/about,我可以看到完整的网站,其中包含布局内的文本。但是 www.example.com/about/ 给出的文本没有任何正文或布局。
  • /about.php?sub= 或 /about.php?sub=about 到 /about/ 是目标。
  • 首先,您需要更清楚地解释您的问题。在这些规则之前,您的 URL 究竟是什么? http://site.com/about.php?sub=about 是一个有效的 URL 吗?如果你输入`/about.php?sub=about`,它应该变成/about,但是是否显示正确的内容取决于图像、css、js包含。
  • 是的,现在 /about.php?sub=about 重定向到 /about/ 和 /about.php 也重定向到 /about/。首先它没有。之前 about.php 显示正文内的文本,现在重定向到 /about/ 它没有。
  • 之前有效的 url 是 example.com/about/ 并给出了完整的网站并将 sub=about 中的文本放入带有 php 包含功能的网站中。由于 apache 更新,当我转到 example.com/about/ 时,我只看到没有布局的纯文本,我希望这能带来一些清晰。
猜你喜欢
  • 2023-03-19
  • 1970-01-01
  • 2014-08-04
  • 1970-01-01
  • 2014-02-19
  • 1970-01-01
  • 2017-12-05
  • 2014-02-28
相关资源
最近更新 更多