【问题标题】:htaccess to change urlhtaccess 更改网址
【发布时间】:2012-09-15 05:40:10
【问题描述】:

我的 .htacess 中有以下代码,但它不能正常工作。是不是因为mod-rewrite没有“开启”,如果是,我该如何检查?

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\$ $1.php [nc]

我想重命名我的地址,例如:

http://www.abc.com -> http://www.abc.com

http://abc.com -> http://www.abc.com

http://www.abc.com/123.html -> http://www.abc.com/123

http://www.abc.com/12-12-12.html -> http://www.abc.com/12-12-12

http://subdomain.abc.com/123.html -> http://subdomain.abc.com/123

基本上删除扩展名并确保其 www 完好无损。

已编辑:

改写成

Options +FollowSymlinks
RewriteEngine on
RewriteCond % ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteRule ^(.*).php $1

还是不行

【问题讨论】:

  • 如果我理解正确,您还需要编辑 PHP/HTML 文件中的 URL。 URL 重写不会为您更改 URL。
  • 目前,网址是abc.com/123.php,这样做后,我可以说如果我要访问abc.com/123abc.com/123.php,它应该可以工作并自动重定向到@987654334 @?最重要的是,无论哪种方式,当我在那里时,我的主页都会立即给我一个 500 内部错误。

标签: apache .htaccess mod-rewrite permissions


【解决方案1】:

第 1 步。

更改所有链接以从链接中删除 .html。确保您已关闭 Multiviews

Options -Multiviews

第 2 步。

当主机名中缺少 www 时,您需要规则来重定向浏览器

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

第 3 步。

当使用 .html.php 请求 URL 并删除扩展名时,您需要规则来重定向浏览器

RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^\ ]+)\.(php|html?)
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ /%2 [L,R=301]

第 4 步。

如果请求实际上是针对 php 或 html 文件的,您需要规则来在内部重写 URI:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.htm -f
RewriteRule ^(.*)$ /$1.htm [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /$1.html [L]

.htaccess

总而言之,您的 htaccess 中应该有这些规则(放弃您可能已经尝试解决此问题的任何规则):

# Make sure Multiviews is off
Options -Multiviews

# turn on rewrite engine
RewriteEngine On

# Redirect requests that are missing www and not a subdomain
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

# Redirect if requests is for a .htm, .html, or .php
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^\ ]+)\.(php|html?)
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ /%2 [L,R=301]

# rewrite to the proper extension if such a file exists
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.htm -f
RewriteRule ^(.*)$ /$1.htm [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /$1.html [L]

完全是这样:

  • 如果浏览器 URL 地址栏显示:http://example.com/index.html,它将被重定向到 http://www.example.com/index
  • 如果浏览器 URL 地址栏显示:http://www.example.com/index,则会显示 http://www.example.com/index 的内容。 html(或index.php,如果存在)。地址栏保持不变。
  • 如果浏览器 URL 地址栏显示:http://foo.example.com/images/image.png,您将在/images/image.png 看到图像。地址栏保持不变。
  • 如果浏览器 URL 地址栏显示:http://foo.example.com/path/foo/bar.php,它将被重定向到 http://foo.example。 com/path/foo/bar
  • 如果浏览器 URL 地址栏显示:http://foo.example.com/path/foo/bar,它将显示 http://foo.example 的内容。 com/path/foo/bar.php。地址栏保持不变。

【讨论】:

    【解决方案2】:

    删除.html

    RewriteRule ^(.*).html $1 [L,QSA]
    

    添加www.

    RewriteCond % ^example.com [NC]
    RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
    

    【讨论】:

    • 我在 RewriteCond %{HTTP_HOST} 上有选项 +FollowSymlinks RewriteEngine !^www\。 RewriteRule ^(.*)$ %{HTTP_HOST}/$1 [R=301,L] RewriteRule ^(.*).html $1 它给了我 500 个内部服务器错误
    • 它仍然返回内部 500...查看我上面的问题以了解我使用的最新代码。
    • 我尝试了代码,没有错误。您在使用<IfModule mod_rewrite.c> </IfModule> 吗??
    • 我尝试用 封装代码,而没有。我仍然 gte 500 内部错误。 akashijap.com.sg/secret
    【解决方案3】:

    您的网站位于哪个主机上?您确定启用了 mod-rewrite 吗?如果未激活,您是否联系过您的主机安装它?

    androbin (https://stackoverflow.com/a/12553613/1713771) 之前发布的解决方案是正确的。

    【讨论】:

      【解决方案4】:

      我使用的.htaccess 文件:

      <IfModule mod_rewrite.c>
          RewriteEngine On
          RewriteCond %{HTTPS} !=on
          RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
          RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteCond %{REQUEST_FILENAME}\.html -f
          RewriteRule ^(.*)$ $1.html
      </IfModule>
      

      这会删除.html 文件扩展名并强制www

      【讨论】:

      • .html 更改为.php 将删除.php 文件扩展名。
      猜你喜欢
      • 2014-06-12
      • 2013-01-20
      • 2016-07-17
      • 1970-01-01
      • 1970-01-01
      • 2019-02-09
      相关资源
      最近更新 更多