简体   繁体   English

Mod_Rewrite并发症

[英]Mod_Rewrite Complications

I have recently been doing a lot of redesign on my website, and one of these changes is SEO URL's, and I am doing Mod_Rewrite to do this. 最近,我在网站上进行了很多重新设计,其中之一是SEO URL,而我正在执行Mod_Rewrite。 The problem is that I am not exactly familiar with reg expressions at all, nor am I exactly the best in the world when it comes to working with the .htaccess file.. Now, the problem I am having is that I have several variables on the same level and the RewriteRules I am using are cancelling each other out. 问题是我完全不熟悉reg表达式,在使用.htaccess文件时我也不是世界上最好的。.现在,我遇到的问题是我有几个变量相同级别和我正在使用的RewriteRules正在互相抵消。 I did find a work around by adding is tag, name, and page into the rewritten url, but that is not what I want. 我确实找到了解决方法,即将标记,名称和页面添加到重写的url中,但这不是我想要的。 I would like the urls to be structured so it is /$lg/articles/[$tag|$name|$page]/ Again,doing is the way I want causes them to conflict because the document doesn't seem to know to read and process off three at the same time. 我希望将URL结构化,使其为/ $ lg / articles / [$ tag | $ name | $ page] /同样,这样做是我想要的方式,导致它们发生冲突,因为文档似乎不知道同时读取并处理三个。 Any tips and suggestions on how to do this? 有关如何执行此操作的任何提示和建议? I would greatly appreciate it. 我将不胜感激。 :D :D

RewriteRule ^([^/]*)/articles/tag/([^/]*)$ /articles.php?lg=$1&tag=$2
RewriteRule ^([^/]*)/articles/name/([a-zA-Z]+)$ /articles.php?lg=$1&name=$2
RewriteRule ^([^/]*)/articles/page/([0-9]+)$ /articles.php?lg=$1&page=$2

What I am wanting it so have the URL's structure like so : /language/articles/Variable Value. 我想要的是具有这样的URL结构:/ language / articles / Variable Value。 Say an integer is put in, I want the url to look like so: /en/articles/1 and it will then act as the page number for the pagination system. 假设输入了一个整数,我希望该URL看起来像这样:/ en / articles / 1,然后它将用作分页系统的页码。 Or an article name is put in: /en/articles/random_article_name and that name is then used to all the specific article in a database. 或者将文章名称放在:/ en / articles / random_article_name中,然后将该名称用于数据库中的所有特定文章。 The same idea applies for the tags, but pulls all articles with that tag. 同样的想法也适用于标签,但是会提取所有带有该标签的文章。 The idea is to have the URLs short and clean. 这样做的目的是使URL短而整洁。 The problem is that if I do the code like so: 问题是,如果我像这样做代码:

RewriteRule ^([^/]*)/articles/([^/]*)$ /articles.php?lg=$1&tag=$2
RewriteRule ^([^/]*)/articles/([a-zA-Z]+)$ /articles.php?lg=$1&name=$2
RewriteRule ^([^/]*)/articles//([0-9]+)$ /articles.php?lg=$1&page=$2

Only one of the $_GET values will be read, and the rest will be ignored regardless of the variable name or regex expression. $ _GET值中只有一个会被读取,其余的将被忽略,无论变量名或正则表达式如何。 I am looking for a way to format my urls like so, if possible. 如果可能,我正在寻找一种格式化网址的方法。 If it isn't, I understand. 如果不是,我了解。 I just figured it doesn't hurt to ask those who are more experienced. 我只是想问问那些更有经验的人并没有什么害处。

I'm not sure what's not working as your rules seem good to me. 由于您的规则对我来说似乎不错,因此我不确定什么是行不通的。 You can try this one to group the 3 rules : 您可以尝试将这三个规则分组:

RewriteRule ^([^/]*)/articles/(page|name|tag)/([0-9]+)$ articles.php?lg=$1&$2=$3

You can have your rules like this: 您可以具有如下规则:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^([^/]*)/articles/([0-9]+)/?$ /articles.php?lg=$1&page=$2 [L,QSA]

RewriteRule ^([^/]*)/articles/([a-zA-Z]+)/?$ /articles.php?lg=$1&name=$2 [L,QSA]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM