使用.htaccess文件网址更名 [英] Url renaming using .htaccess file

查看:126
本文介绍了使用.htaccess文件网址更名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数是重定向和删除文件扩展名或动态网址。

Most are for redirections and removing file extensions or for dynamic urls.

这个问题是我不能让一个简单的静态的URL来重命名

The problem being I can't get a simple static url to rename

Options +FollowSymlinks
RewriteEngine On
RewriteRule ^fileThathasalongname file.html

我现在有什么mysite.co.uk/fileThathasalongname.html

我要的是mysite.co.uk/file/,而文件= file.html

使用:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^FileIWantToChange.html FriendlyNamed.html

使用这个给我的错误多的选择

Using this gives me the error multiple Choices

+++++++++++++++++编辑+++++++++++++++++++++++++

+++++++++++++++++Edit+++++++++++++++++++++++++

想我会加入我的最终版本,供人瞻仰的,它可以帮助,下面anwser是正确的。

Thought i'd add my final version for people to look at, it may help, the anwser below is correct.

Options +FollowSymLinks -MultiViews
DirectorySlash Off

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]

RewriteRule ^FriendlyNamed.html FileIWantToChange.html [L]

RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]

RewriteCond %{HTTP_HOST} ^www.mysire.co.uk [NC]
RewriteRule ^(.*)$ http://mysite.co.uk/$1 [L,R=301]

一切工作一个魅力!

all works a charm!

推荐答案

我看到多个问题怎么回事。首先常规EX pression在匹配友好的URL的用户类型。因此,你需要换你友好的URL和文件路径彼此。友好的或假的名字居左,而URL重定向到默默居右。另外,还要确保你已经设置的目录基础 / 。最后,这是很好的添加 [L] 来执行它在案件在同一个文件什么的最后一个规则试图改写的路径。由于注意到其它的htaccess文件低了下去,这取决于文件的位置,也将甚至强制执行规则时检查有最后一个规则。同时垃圾级的选项部分完全。试试这个:

I see multiple issues going on. Firstly the regular expression is matched against the friendly URL the user types in. So you need to swap your friendly url and file path with each other. The friendly or "fake" name goes on the left while the url to redirect to silently goes on the right. Also make sure you've set the directory base to /. Finally it's good to add an [L] to enforce it to be the last rule in case anything in the same file tries to rewrite the path. Due note that other htaccess files lower down, depending on the files location, will also be checked even when enforcing the rule has the last rule. Also junk the options part completely. Give this a try:

RewriteBase /
RewriteEngine On
RewriteRule ^FriendlyNamed.html FileIWantToChange.html [L]

这篇关于使用.htaccess文件网址更名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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