Php 如何从URL中提取变量部分,以及如何知道URL变量是否由数字附加?
我有这样一个URL:Php 如何从URL中提取变量部分,以及如何知道URL变量是否由数字附加?,php,.htaccess,mod-rewrite,Php,.htaccess,Mod Rewrite,我有这样一个URL: www.example.com/education/edu.php?Main=Colleges&Category=Engineering-colleges www.example.com/education/Colleges/$Name-sqr$Id 我使用.htaccess www.example.com/education/Colleges/Engineering-colleges 我在同一教育目录中还有另一个URL,如: www.example.com/
www.example.com/education/edu.php?Main=Colleges&Category=Engineering-colleges
www.example.com/education/Colleges/$Name-sqr$Id
我使用.htaccess
www.example.com/education/Colleges/Engineering-colleges
我在同一教育
目录中还有另一个URL,如:
www.example.com/education/eduS.php?Main=Colleges&CollegeId=$Id&CollegeName=$Name
我想重写此URL,如下所示:
www.example.com/education/edu.php?Main=Colleges&Category=Engineering-colleges
www.example.com/education/Colleges/$Name-sqr$Id
如何做到这一点?我想不出一种方法来做到这一点,因为上面两个重写URL具有相同数量的参数。所以我无法将它们重定向到正确的页面
以下是我的问题:
- 有没有办法使用
验证带有.htaccess
的URL变量,并重定向到sqr$Id
页面李>eduS.php
- 如何从该URL提取
,并使该变量用于该页面中的操作李>Id
(\w+)\-sqr(\d+)
顺便问一下,使用id名称看起来不是更好吗
(\d+)\-(\w+)
尝试:
在“教育”文件夹中。它应该在重写为edu.php
的另一条规则之前。如果您的规则位于文档根目录中,则需要如下所示:
RewriteRule ^education/([^/]+)/([^/]+)-sqr([0-9]+)$ /education/eduS.php?Main=$1&CollegeId=$3&CollegeName=$2 [L]
在您的
/education/.htaccess
中尝试此代码:
RewriteEngine On
RewriteBase /education/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^-]+)-sqr([0-9]+)/?$ eduS.php?Main=$1&CollegeId=$3&CollegeName=$2 [L,QSA,NC]
RewriteRule ^([^/]+)/([^/]+)/?$ eduS.php?Main=$1&Category=$2 [L,QSA]
你能发布你现有的.htaccess代码吗?@anubhava
在RewriteCond%{REQUEST_FILENAME}-f[OR]
RewriteCond%{REQUEST_FILENAME}-d
RewriteRule.+-[L]
RewriteRule^(.*)/(.*)$eduS.php?Main=$1&String=$2[L,NC,QSA]
RewriteRule^(.*)/(.*)$edu.php?Main=$1&Category=$2[L,NC,QSA]/+[sq0-]+)/本规则中的“$为什么“/?”(在末尾)。。有必要吗?这是为了精确匹配
/Colleges/$Name sqr$Id
零件。这个正则表达式有什么问题吗?哦,没问题,你可以问/澄清任何数量的问题。我只是在检查答案是否对你们有用,很高兴知道它对你们有用。