Php 如何从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/

我有这样一个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/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
    验证带有
    sqr$Id
    的URL变量,并重定向到
    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
零件。这个正则表达式有什么问题吗?哦,没问题,你可以问/澄清任何数量的问题。我只是在检查答案是否对你们有用,很高兴知道它对你们有用。