需要删除.html文件扩展名和重复的名称 [英] Need to remove .html file extension and duplicate names

查看:185
本文介绍了需要删除.html文件扩展名和重复的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有下面内容的.htaccess文件,它删除了我所有网站页面的.html文件扩展名。

  Options + MultiViews 
RewriteEngine On
RewriteCond%{REQUEST_FILENAME}!-d
RewriteCond%{REQUEST_FILENAME}!-f
RewriteRule ^([^ \。] +) $ $ 1.html [NC,L]
RewriteEngine在
RewriteCond%{SERVER_PORT}!= 443
RewriteCond%{HTTP_HOST} ^(www \。)?james-lee\。 io $ [NC]
RewriteRule ^ $ https://www.james-lee.io% {REQUEST_URI} [R,L]

现在我的链接看起来像www.james-lee.io/resume/resume,但它们之前看起来像www.james-lee.io/resume/resume.html。我想删除文件夹名称,以便文件名不会被文件名减去.html,最终结果如www.james-lee.io/resume。



我看到了类似的问题,但并不完全符合我的要求。

解决方案

所以我完成了这个任务!
试试这段代码:

  RewriteCond%{REQUEST_URI} ^ /(。*)/(。*)$ 
RewriteCond%{DOCUMENT_ROOT} /%1 -d
RewriteCond%{DOCUMENT_ROOT} /%1 /%2 -f
RewriteCond%1 ::%2 ^(。*):: \ 1 $
RewriteRule ^(。*)$ /%1 [R,L]

RewriteCond%{REQUEST_FILENAME} -d
RewriteRule ^(。*)/(。* )$ / $ 1 / $ 1 [END]

现在我试着解释这个规则。


  1. 第一行:您要求/文件夹/文件

  2. 第二行:检查/ folder / real存在

  3. 第三行:检查/文件夹/文件是否是真实存在的文件第四行:我使用表示法%1 ::%2,因为反向引用在RewriteCond的左边部分只能使用
    。但是可以重用右边部分的
    左边部分。因此,在^(。*):: \\ 1 $I
    之前检查所有::。然后我得到了反向引用的结果。因此,
    如果文件夹等于文件,则::之后的文本将等于%2。

  4. 接下来我只是重定向到结果(/文件夹或/文件,doesn 't
    很重要,因为两者都是相同的)

但是,如果文件夹==文件,重定向将始终保存到目录。
所以,接下来我检查一下,如果重定向结果是现有的dir并更改链接。

请求示例
http:// yourdomain / test / test



(这将重定向到 http:// yourdomain / test ,但会引用原始链接)



我希望我解释清楚,但如果您有任何问题,我很乐意回答。

谢谢你的任务!



PS另请参阅 RewriteCond中的%N反向引用



<强>已更新即可。您的htaccess必须如下所示:

  RewriteEngine on 

RewriteCond%{SERVER_PORT}!= 443
RewriteCond%{HTTP_HOST} ^(www \。)?james-lee\.io $ [NC]
RewriteRule ^ $ https://www.james-lee.io% {REQUEST_URI} [R,L]

RewriteCond%{REQUEST_URI} ^ /(。*)/(。*)$
RewriteCond%{DOCUMENT_ROOT} /%1 -d
RewriteCond% {DOCUMENT_ROOT} /%1 /%2 \.html -f
RewriteCond%1 ::%2 ^(。*):: \ 1 $
RewriteRule ^(。*)$ /% 1 [R,L]

RewriteCond%{REQUEST_FILENAME} -d
RewriteRule ^(。*)/(。*)$ /$1/$1.html [END]

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


I have a .htaccess file with the contents below, that removes the .html file extension for all of my website's pages.

Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteCond %{HTTP_HOST} ^(www\.)?james-lee\.io$ [NC]
RewriteRule ^$ https://www.james-lee.io%{REQUEST_URI} [R,L]

My links now look like www.james-lee.io/resume/resume when before they looked like www.james-lee.io/resume/resume.html. I would like to remove the folder name so the name of the folder is not duplicated by the name of the file minus the .html and the final result looks like www.james-lee.io/resume.

I have seen similar questions but not exactly what I am looking for.

解决方案

So I have done this task! Try this code:

RewriteCond %{REQUEST_URI} ^/(.*)/(.*)$
RewriteCond %{DOCUMENT_ROOT}/%1 -d
RewriteCond %{DOCUMENT_ROOT}/%1/%2 -f
RewriteCond %1::%2 ^(.*)::\1$
RewriteRule ^(.*)$  /%1 [R,L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)/(.*)$ /$1/$1 [END]

Now I try to explain this rules.

  1. first line: you do request like /folder/file
  2. second line: check if /folder/ real existing folder
  3. third line: check if /folder/file is real existing file
  4. fourth line: I use notation %1::%2 because backreferences can only be used in the left part of RewriteCond. But it possible to reuse left part in pattern of the right part. So, in the "^(.*)::\1$" I check all before ::. Then I have result at the \1 backreference. So, if folder is equal to file, text after :: will be equal to %2.
  5. Next I just redirect to the result (/folder or /file, doesn't matter, because both are equal)

But if folder == file, redirect will be always to the directory. So, next I check, if redirect result is existing dir and change the link.

Request example: http://yourdomain/test/test

(this will be redirected to http://yourdomain/test, but will reference to original link)

I hope, I explain clearly, but if you have any questions, I would glad to answer.

Thank you for insteresting task!

P.S. see also %N backreference inside RewriteCond

UPDATED. Your htaccess have to be like below:

RewriteEngine on

RewriteCond %{SERVER_PORT} !=443
RewriteCond %{HTTP_HOST} ^(www\.)?james-lee\.io$ [NC]
RewriteRule ^$ https://www.james-lee.io%{REQUEST_URI} [R,L]

RewriteCond %{REQUEST_URI} ^/(.*)/(.*)$
RewriteCond %{DOCUMENT_ROOT}/%1 -d
RewriteCond %{DOCUMENT_ROOT}/%1/%2\.html -f
RewriteCond %1::%2 ^(.*)::\1$
RewriteRule ^(.*)$  /%1 [R,L]

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

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

这篇关于需要删除.html文件扩展名和重复的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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