url 使用 htaccess 重写 html 和 javascript 中的单个文件

url rewriting using htaccess for single files in html and javascript

我的第一个问题是"can i use html files for url parameter rewriting and handle the parameter in javascript?"

第二个问题是: 我该怎么办?,如果我想重写这些网址:

*localhost OR sample.com*/blog.html
*localhost OR sample.com*/about.html
*localhost OR sample.com*/articles.html?title=sample-name-parameter

这些网址:

*localhost or sample.com*/blog/
*localhost or sample.com*/about/
*localhost or sample.com*/articles/sample-name-parameter

这是我的 htaccess 文件:

DirectoryIndex blog.html index.html

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ .html
RewriteRule ^([^/]+)/([^/]+)/$ //.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ // [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^articles/([^/]+)$ /articles/.html?p= [L]
</IfModule>

实际上你不能,但你可以通过分割当前路径来完成,用window.location.pathname获取当前路径,和平使用split分割路径。

var path = window.location.pathname, // "/article/something"
    peaces = path.split(),
    title = peaces[1]; // "something"

并试试这个 htaccess 代码:

DirectoryIndex blog.html index.html

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/$ .html
RewriteRule ^articles/([^/]+)$ /articles/articles.html?title= [L]
</IfModule>