【问题标题】:Mod_rewrite to remove PHP extension and redirectMod_rewrite 删除 PHP 扩展和重定向
【发布时间】:2017-05-24 12:31:08
【问题描述】:

我已经阅读并测试了许多解决方案,但无法正常工作。

我需要什么?

如果用户在地址栏输入.../etc.php应该重定向到.../etc/,同样.../etc/应该可以直接访问,一些例子:

www.mysite.com/index.php->www.mysite.com/

www.mysite.com/register.php->www.mysite.com/register/

www.mysite.com/profile.php?id=123->www.mysite.com/profile/123/

如果lang参数设置为en(仅en,否则地址栏不显示lang值)它应该在root之后添加/en/,类似于:

www.mysite.com/register.php?lang=en->www.mysite.com/en/register/

www.mysite.com/index.php?lang=en->www.mysite.com/en/


我的尝试

Options -MultiViews

RewriteEngine on
RewriteBase /

# url is ONLY '/en' or '/de' -> redirect to /en/ or /lt/ (adding slash)
RewriteRule ^(en|lt)$  $1/ [R=301,L]

# now all urls have en/ lt/ -> parse them
RewriteRule ^(en|lt)/(.*)$  $2?lang=$1&%{query_STRING} [L]
# no "R=301" here --------------------------------------^

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

但上面的代码允许用户访问www.mysite.com/register.phpwww.mysite.com/register/,而它应该从www.mysite.com/register.php 重定向(更改网址)到www.mysite.com/register/

【问题讨论】:

  • 试试这个条件RewriteCond %{REQUEST_FILENAME}\.php -f
  • @Nawin 如果我在.htaccess 文件的底部附加RewriteCond %{REQUEST_FILENAME}\.php -f - 什么也不会发生。如果我删除所有其他规则,只留下RewriteCond %{REQUEST_FILENAME}\.php -f,那么我可以访问www.mysite.com/register.php,但不能重定向,也不能再访问www.mysite.com/register/,所以这不起作用。
  • RewriteRule ^([^\.]+)$ $1.php [R=301,L] 我想你想删除你的扩展,所以你可以试试这个
  • @Nawin 我已经用RewriteRule ^([^\.]+)$ $1.php [R=301,L] 替换了RewriteRule ^([^\.]+)$ $1.php [NC,L],但还是一样,没有删除.php 扩展。
  • @anubhava 是的,可能有多个参数。

标签: php apache .htaccess redirect mod-rewrite


【解决方案1】:

我建议使用从 URL 中删除 .php 扩展名和 index.php 的重定向规则:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} /+(.*?/)?(?:index)?(.*?)\.php[/\s?] [NC]
RewriteRule ^ %1%2/ [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

【讨论】:

  • 感谢您的回答,但看起来它不起作用。我试图删除我的所有代码,并在Options -MultiViews RewriteEngine on RewriteBase / RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[/\s?] [NC] RewriteRule ^ /%1%2/ [R=302,L,NE] 中留下你的建议,但没有工作。我现在可以访问www.mysite.com/etc,只使用PHP 扩展,例如:www.mysite.com/etc.php。我试图将其附加到文件末尾,但是以这种方式如果我访问www.mysite.com/etc 它会添加.php,而不是删除扩展名
  • 如果我尝试访问www.mysite.com/etc/,它会重定向到www.mysite.com/etc/.php。它应该是反向的,删除.php 扩展,而不是附加。
  • 现在我收到Internal Server Error. The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator at [no address given] to inform them of the time this error occurred, and the actions you performed just before this error. More information about this error may be available in the server error log. 这个.htaccess 文件存储在public_html (root) 与index.php 相同的目录中,我只保留了您更新的代码。
  • 我检查了一个错误日志,我得到了这个:/public_html/.htaccess: Either all Options must start with + or -, or no Option may.
  • 我问了另一个问题,要清楚。这一个接受为正确答案。非常感谢您的帮助。另一个问题在这里:stackoverflow.com/questions/44184455/…
猜你喜欢
  • 2013-10-11
  • 2015-06-29
  • 1970-01-01
  • 1970-01-01
  • 2016-01-06
  • 2019-02-12
  • 1970-01-01
  • 2018-06-25
  • 2012-01-03
相关资源
最近更新 更多