.htaccess的正则表达式不匹配 [英] Htaccess Regex won't match

查看:211
本文介绍了.htaccess的正则表达式不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个快速提示迫切需要。

尝试使用htaccess的改变这种不那么可爱的网址

 的http://localhost/test/index.php页=文章及放大器;条款ArticleID = 61
 

 的http://本地主机/测试/条/ 2015年9月21日 - 这,是最头条
 

从我收集到我需要的最后一部分发送到PHP脚本,然后可以从数据库中获取匹配的ID。我知道我应该能够给用户发送到原始URL往上顶。

 重写规则^(。*)\ /条\ /(。*)$重定向/ article.php [L]
#重写规则^(。*)$的index.php
 

截至目前我没有传递信息的脚本呢。 重定向/ article.php 的只包含一个print语句,让我知道,一旦我得到那么远。

不过,尽管我的大脑和每一个正则表达式调试器说,否则也不会在第二个code中提供的URL匹配。所有我得到的是好老404。如果我启动了第二条规则也适用于我的网址,告诉我说,第一个是简单地被跳过。

我在想什么?


的.htaccess:

 < IfModule mod_rewrite.c>
    RewriteEngine叙述上

    #重命名个人网页
    重写规则^(。*)\ /条\ /(。*)$重定向/ article.php [L]
    #重写规则^(。*)$的index.php

    #调整图像
    的RewriteCond%{} REQUEST_FILENAME!-f
    重写规则^ * \ /([0-9] +)\  - (([0-9] | [AZ])+)(。)?\(preV)$文件preview.php ID = $ 2及大小= $ 3 [L]

    php_value的upload_max_filesize 20M
    php_value的post_max_size 21M
< / IfModule>
 

解决方案

.htaccess文件的位置告诉你如何必须列出mod_rewrite的路径。里面的.htaccess,不与领先的 / 收到重写规则路径。既然你是居住在 /测试(。*)在您的规则的开局并不匹配任何东西无害。自那之后是 / 文章/ 路径期待 / 它永远不会接受。最简单的解决办法是改变这种规则,以匹配文章在开始通过:

 重写规则^文章/(.*)重定向/ article.php [L]
 

假设你会使用这个作为PHP脚本查找,添加一个参数使用 $ 1 捕获的图案,如:

 重写规则^文章/(.*)重定向/ article.php?文章= $ 1 [L]
 

I'm in desperate need of a quick tip.

Trying to use htaccess to change this not so lovely url

http://localhost/test/index.php?page=Article&articleID=61

to

http://localhost/test/article/2015-09-21-this-is-the-headline

From what I've gathered I need to send the last part to a php script which can then get the matching id from the database. Knowing that I should be able to send the user to the original url up top.

RewriteRule ^(.*)\/article\/(.*)$ redirect/article.php [L]
# RewriteRule ^(.*)$ index.php

As of right now I'm not passing the information to the script yet. redirect/article.php only contains a print statement to let me know once I get that far.

However, despite my brain and every regex debugger saying otherwise, it won't match the url provided in the second code box. All I'm getting is the good old 404. If I activate the second rule it is applied to my url, telling me that the first one is simply being skipped.

What am I missing?


.htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On

    # rename individual pages
    RewriteRule ^(.*)\/article\/(.*)$ redirect/article.php [L]
    # RewriteRule ^(.*)$ index.php

    # resize images    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.)*\/([0-9]+)\-(([0-9]|[a-z])+)\.(prev)$ filePreview.php?id=$2&size=$3 [L]

    php_value upload_max_filesize 20M
    php_value post_max_size 21M
</IfModule>

解决方案

The location of a .htaccess file informs how you must list paths for mod_rewrite. Inside .htaccess, paths for RewriteRule are not received with a leading /. Since yours was residing in /test, the (.*) at the start of your rule wasn't matching anything and harmless. Since that was followed by /, the article/ path was expecting a / it would never receive. The simplest fix is to change this rule to match article at the start via:

RewriteRule ^article/(.*) redirect/article.php [L]

Assuming you'll use that as a lookup in the PHP script, add a parameter to use the $1 captured pattern like:

RewriteRule ^article/(.*) redirect/article.php?article=$1 [L]

这篇关于.htaccess的正则表达式不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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