Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache 将丑陋的URL重定向到其更干净的版本,并/或同时重写它们_Apache_Mod Rewrite_Redirect - Fatal编程技术网

Apache 将丑陋的URL重定向到其更干净的版本,并/或同时重写它们

Apache 将丑陋的URL重定向到其更干净的版本,并/或同时重写它们,apache,mod-rewrite,redirect,Apache,Mod Rewrite,Redirect,我一直在寻找改变http://www.mysite.com/about.html至http://www.mysite.com/about但到目前为止运气不佳 这可能吗?尝试将其添加到您的.htaccess中: RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.html -f RewriteRule ^(.*)$ $1.html 第2-3行处理原始请求包含.html的情况。此

我一直在寻找改变
http://www.mysite.com/about.html
http://www.mysite.com/about
但到目前为止运气不佳


这可能吗?

尝试将其添加到您的.htaccess中:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
第2-3行处理原始请求包含.html的情况。此请求通过
[R=301]
标志转换为301重定向,并通过
[L]
标志终止进一步处理

第4行作为第5行的条件。第5行执行正常的URL重写。我只是将.html附加到任何URL,而不是以.html结尾

理解递归: 如果没有
RewriteCond
[L]
标志,规则将创建一个无限循环

  • 像about.html这样的请求在第3行变成about,在第5行变成about.html<代码>[R]防止了这种情况
  • 第5行的请求(如about将变为about.html),然后mod_rewrite再次处理此URL,以便第3行将其更改回about;但是,
    RewriteCond
    会阻止这样做,因为它会检查浏览器发送的原始请求,而不是重新编写的请求

  • 在哪里换车?在php/html代码中?否,通过.htaccess更改URL尝试检查文件路径等。。。在my LocalHost上运行良好你说的检查文件路径是什么意思?仍然没有做任何事情,我在htaccess文件中设置了一些条件,这样,如果有人使用mysite.com,它会自动更改为www.mysite.com,所以我肯定会将它放在正确的位置。。感谢到目前为止的帮助。我看到了“关于”页面,但我想要的是,如果用户将mysite.com/about.html添加到书签中,并单击地址栏上显示的书签mysite.com/about而不是mysite.com/about.htmlOh,这是另一回事。它被称为301 redirect via.htaccess,基本上是通过另一种方式实现相同的规则。这几乎总是导致循环/无限重定向,并且非常棘手。我会看看是否能找到解决方案。请注意,我没有太多的页面,所以我很乐意为每个页面编写规则,例如将about.html改为/about-contact.html改为/contact等。谢谢again@Bob当前位置我已编辑了我的答案。似乎在我的系统上工作。
    RewriteEngine On
    RewriteCond %{THE_REQUEST} \.html
    RewriteRule ^(.+)\.html$ /$1 [R=301,L]
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule ^(.+)$ $1.html