Regex 需要.htaccess中的特定URL重写规则

Regex 需要.htaccess中的特定URL重写规则,regex,apache,.htaccess,mod-rewrite,rewrite,Regex,Apache,.htaccess,Mod Rewrite,Rewrite,我刚开始设置.htaccess,如果您能帮我解决这个问题,我将不胜感激 如果用户点击以下URL(原始URL): 我希望它重定向到: 并将原始URL保留在浏览器中 到目前为止,我有以下几点: RewriteRule ^somepage/(.*)$ /somepage [R=301] 但是,它不起作用 我怎样才能让它工作 编辑: 下面是我的.htaccess文件现在的样子: # do not allow anyone else to read your .htaccess file <F

我刚开始设置.htaccess,如果您能帮我解决这个问题,我将不胜感激

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

我希望它重定向到:

并将原始URL保留在浏览器中

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

RewriteRule ^somepage/(.*)$ /somepage [R=301]
但是,它不起作用

我怎样才能让它工作

编辑:

下面是我的.htaccess文件现在的样子:

# 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]
#不允许任何其他人读取您的.htaccess文件
全盘否定
#禁止查看目录
选项所有-索引
#在列出目录时隐藏此文件列表以防被看到
IndexIgnore.htaccess*/.??**~*#*/HEADER**/README**/\u vti*
#禁用服务器签名-有助于执行
服务器签名关闭
重新启动发动机
#example.com/page将显示example.com/page.html的内容
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
RewriteCond%{REQUEST_FILENAME}.html-f
重写规则^(+)$$1.html[L,QSA]
#301从example.com/page.html到example.com/page
重写cond%{THE_REQUEST}^[A-Z]{3,9}\/.\.html\HTTP/
重写规则^(.*)\.html$/$1[R=301,L]
重写规则^somepage/(.*)$/somepage[R=301]

这条规则就是问题所在:

RewriteRule ^somepage/(.*)$ /somepage [R=301]
原因有二:

  • 因为您不想更改URL,所以在此规则中不能使用
    R=301
    标志
  • 更大的问题是您的正则表达式
    ^somepage/(.*)$
    也将匹配
    /somepage/
    ,并且您需要匹配
    /somepage/something
  • 要解决此问题,请按以下方式使用您的.htaccess:

    # 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文件
    全盘否定
    #禁止查看目录
    选项所有-索引
    #在列出目录时隐藏此文件列表以防被看到
    IndexIgnore.htaccess*/.??**~*#*/HEADER**/README**/\u vti*
    #禁用服务器签名-有助于执行
    服务器签名关闭
    重新启动发动机
    重写基/
    #301从example.com/page.html到example.com/page
    重写cond%{THE_REQUEST}^[A-Z]{3,9}\/.\.html\HTTP/[NC]
    重写规则^(+?)\.html$/$1[R=301,L]
    RewriteCond%{DOCUMENT_ROOT}/$1\.html-f[NC]
    重写规则^([^/]+)$1.html[L]
    
    Rahul,您不能在浏览器中重定向并保留相同的URL。如果您有一个规则来处理对
    /somepage
    的请求,那么您只需删除
    R=301
    。还建议将其替换为
    L
    ,以表明如果找到匹配项,它将不会继续检查规则。编辑:也许你想共享你的
    .htaccess
    文件,这样我们就可以在上下文中查看所有内容了?@mikeanshony我在上面的问题中添加了.htaccess文件。我在这里问这个问题是因为我的朋友提出了这样一个想法,即显示一个URL,而在幕后有另一个URL处于活动状态,但他记不起重写规则。对不起,这不起作用。当我点击原始URL时,我收到
    500内部服务器错误
    。在我的项目中,
    somepage
    实际上是
    cord blood basics
    ,所以我在正则表达式中尝试了
    cord blood basics
    cord-blood-basics
    ,但这也不起作用。我也尝试过在开始时不使用
    ^
    ,这也不起作用。
    somepage
    不是一个真正的目录,它是一个HTML文件
    something
    是这个HTML页面的手风琴中的按钮,当点击这个URL时,我试图通过编程方式单击它。我知道这对你来说可能听起来很奇怪,但是如果你提到这个,你会更好地理解上下文。而且,这是我项目中唯一的.htaccess文件。在
    xampp\apache\logs\error.log
    中,这是我点击该URL后得到的:
    [Fri Feb 13 21:25:18.260038 2015][core:error][pid 6176:tid 1692][client 127.0.0.1:55869]AH00124:由于可能的配置错误,请求超出了10个内部重定向的限制。如有必要,使用“LimitInternalRecursion”增加限制。使用“LogLevel debug”获取回溯信息。
    现在它给了我一个404错误。当我查看dev工具的Network选项卡时,我看到它正在查找HTML类型的file
    东西。Apache错误日志中没有错误。我的大部分答案也包含在您的问题中。如果您的问题是关于
    RewriteCond%{DOCUMENT\u ROOT}/$1\.html-f[NC]
    ,那么它是在检查
    .html
    文件的存在
    %{DOCUMENT_ROOT}
    是web根目录的Apache属性,
    $1
    是从
    重写规则
    中的匹配组#1捕获的值。