本文介绍了需要在.htaccess特定的URL重写规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新设立的.htaccess,我会AP preciate在我的问题有所帮助。

如果用户点击以下网址(原始URL):

http://www.example.com/somepage/something

我希望它重定向到:

http://www.example.com/somepage

和保持原来的网址在浏览器中。

到目前为止,我有以下几点:

 重写规则^ SomePage的/(.*)$ / SomePage的[R = 301]
 

但是,它不工作。

我如何得到这个工作?

编辑:

下面是我的.htaccess文件看起来像现在:

 #不允许任何人读你的.htaccess文件
<文件的.htaccess>
所有否认
< /文件>

#禁止观看目录
选项​​所有-Indexes

#隐藏的文件列表中,从被视为列出一个目录时,
IndexIgnore的.htaccess * /。?? *〜*#* / HEADER * * / README * * / _ VTI *

#禁用服务器签名工具使用preformance帮助
ServerSignature关闭

RewriteEngine叙述上
#example.com /页会显示example.com/page.html内容
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{} REQUEST_FILENAME html的-f
重写规则^(。+)$ $ 1.HTML [L,QSA]

#301 example.com/page.html到example.com/page~~V
的RewriteCond%{THE_REQUEST} ^ [AZ] {3,9} \ /.* \ HTML \ HTTP /
重写规则^(。*)\。HTML / $ 1 [R = 301,L]

重写规则^ SomePage的/(.*)$ / SomePage的[R = 301]
 

解决方案

这条规则的问题是:

 重写规则^ SomePage的/(.*)$ / SomePage的[R = 301]
 

有两个原因:

  1. 既然你不想URL改变你不能在这个规则中使用 R = 301 标记。
  2. 在更大的问题是,你的正则表达式 ^ SomePage的/(.*)$也将同时符合 / SomePage的/ 你需要匹配 / SomePage的/东西

要解决这个问题,有你完整的.htaccess这样的:

 #不允许任何人读你的.htaccess文件
<文件的.htaccess>
所有否认
< /文件>

#禁止观看目录
选项​​所有-Indexes

#隐藏的文件列表中,从被视为列出一个目录时,
IndexIgnore的.htaccess * /。?? *〜*#* / HEADER * * / README * * / _ VTI *

#禁用服务器签名工具使用preformance帮助
ServerSignature关闭

RewriteEngine叙述上
的RewriteBase /

#301 example.com/page.html到example.com/page~~V
的RewriteCond%{THE_REQUEST} ^ [AZ] {3,9} \ /.* \ HTML \ HTTP / [NC]
重写规则^(。+?)\。HTML / $ 1 [R = 301,L]

的RewriteCond%{DOCUMENT_ROOT} / $ 1 \ html的-f [NC]
重写规则^([^ /] +)$ 1.HTML [L]
 

I am new to setting up the .htaccess and I would appreciate some help in my problem.

If the user hits the following URL (original URL):

http://www.example.com/somepage/something

I want it to redirect to:

http://www.example.com/somepage

AND keep the original URL in the browser.

So far, I have the following:

RewriteRule ^somepage/(.*)$ /somepage [R=301]

But, it doesnt work.

How do I get this working?

EDIT:

Here is what my .htaccess file looks like right now:

# do not allow anyone else to read your .htaccess file
<Files .htaccess>
deny from all
</Files>

# forbid viewing of directories
Options All -Indexes

# hide this list of files from being seen when listing a directory
IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*

# disable the server signature- helps with preformance
ServerSignature Off

RewriteEngine On
#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]

#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]

RewriteRule ^somepage/(.*)$ /somepage [R=301]
解决方案

This rule is the problem:

RewriteRule ^somepage/(.*)$ /somepage [R=301]

for two reasons:

  1. Since you don't want URL to change you must not use R=301 flag in this rule.
  2. Bigger problem is that your regex ^somepage/(.*)$ will also also match /somepage/ and you needed to match /somepage/something.

To fix this issue have your full .htaccess like this:

# do not allow anyone else to read your .htaccess file
<Files .htaccess>
deny from all
</Files>

# forbid viewing of directories
Options All -Indexes

# hide this list of files from being seen when listing a directory
IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*

# disable the server signature- helps with preformance
ServerSignature Off

RewriteEngine On
RewriteBase /

#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/ [NC]
RewriteRule ^(.+?)\.html$ /$1 [R=301,L]

RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
RewriteRule ^([^/.]+) $1.html [L]

这篇关于需要在.htaccess特定的URL重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 19:59