为SEO友好的URL重写简单的html文件 [英] rewriting simple html files for SEO friendly URLS

查看:95
本文介绍了为SEO友好的URL重写简单的html文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的文件mydomain.com/business_nottingham.html 我想将其重写为SEO友好的URL,例如mydomain.com/business-nottingham/

I have a simple file mydomain.com/business_nottingham.html and i want to re-write that to an SEO friendly URL, eg mydomain.com/business-nottingham/

我已经搜索了所有示例,但它们似乎是为CMS或PHP脚本设计的.

I've googled all the examples but they seem to be designed for either CMSes or PHP scripts.

是否有一个简单的.htaccess重写示例,该示例可以使我做上述非常简单的事情?

Is there a simple .htaccess re-write example available that allows me to do something very simple as above?

我终于找到了以下代码

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301] 

但是,它仍然无法正常工作.如果我输入整洁的URL,则会重定向到404页面,但是输入确切的.html文件名会使我进入正确的文件,但不会显示干净的URL.

However, it's still not quite working. If i enter the tidy URL, i get re-directed to a 404 page, but putting in the exact .html file name gets me to the right file but doesnt present me with a clean url.

我通过阅读各种文章和教程尝试了上述各种组合,但由于某种原因,它似乎对我不起作用.

I've tried various combinations of the above by reading various articles and tutorials but for some reason it doesn't seem to work for me.

推荐答案

仅当实际请求是对.html文件的请求时,您才想重定向,然后您想在内部重写为html文件. URL解析的方式是浏览器显示它认为要去的地方(地址栏中的URL),然后向Web服务器发出请求.如果Web服务器(htaccess文件所在的位置)想要更改浏览器的URL地址栏中的内容,则需要告诉浏览器从字面上加载完全不同的URL.然后,浏览器将请求新的URL .然后,服务器必须在内部将该URL改写回实际资源所在的位置(第一个URL),但是浏览器看不到这种情况.

You want to redirect only when the actual request is for an .html file, then you want to internally rewrite to the html file. The way URLs resolve is the browser shows where it thinks it's going (URL in the address bar), then the request is made to the web server. If the webserver (where the htaccess file is) wants to change what's in the browser's URL address bar, it needs to tell the browser to literally load an entirely different URL. The browser will then request the new URL. Then the server must internally rewrite that URL back to where the actual resource is (the first URL), but the browser doesn't see this happen.

RewriteEngine On

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

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

这篇关于为SEO友好的URL重写简单的html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆