使用.htaccess[domainname/cms.php?iCmsId=1转换为domainname/about us.php]更改url

使用.htaccess[domainname/cms.php?iCmsId=1转换为domainname/about us.php]更改url,php,.htaccess,Php,.htaccess,我需要更改网页url的显示 我的网页有一个url,比如cms.php?iCmsId=1,cms.php?iCmsId=2,cms.php?iCmsId=3等等。。 现在我想要cms.php?iCmsId=1那么url将是www.test.com/about us.php或www.test.com/about us.html这将把用户重定向到about us.html RewriteRule ^cms.php?iCmsId=1$ /about-us.html [R=301,L] RewriteRu

我需要更改网页url的显示

我的网页有一个url,比如
cms.php?iCmsId=1
cms.php?iCmsId=2
cms.php?iCmsId=3
等等。。
现在我想要
cms.php?iCmsId=1
那么url将是
www.test.com/about us.php
www.test.com/about us.html
这将把用户重定向到
about us.html

RewriteRule ^cms.php?iCmsId=1$ /about-us.html [R=301,L]
RewriteRule ^cms.php?iCmsId=2$ /about-us.html [R=301,L]

我想你对此很感兴趣:

RewriteEngine On
RewriteCond %{REQUEST_URI} .html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^about-us.html cms.php?iCmsId=1 [PT]
这意味着:

  • 激活重写引擎
  • 如果客户端正在请求html页面
  • 如果服务器上不存在该文件
  • 如果url是about us.html,则访问
    cms.php?iCmsId=1
  • 如果您想选择以下内容:

    RewriteRule ^about-us-([0-9]+).html cms.php?iCmsId=$1 [PT]
    
    这将匹配任何url,如:
    about-us-324.html
    ,并实际访问您的页面,如:
    cms.php?iCmsId=324

    这就是你要问的吗


    注:Chris告诉你如何使用.htaccess进行301重定向,.htaccess黑客最强大的技巧之一就是重写URL。这使我们能够对我们的链接进行一些强大的操作;有用的东西,比如将很长的URL转换为短而可爱的URL,将dynamic?generated=页面和URL转换为/friendly/flat/链接,重定向缺少的页面,防止热链接,执行自动语言翻译,等等

    别搞错了,mod_重写很复杂。这不是一个简单的科技小吃的主题,甚至可能不是一个周末的速成班,我见过一些家伙用mod_rewrite完成了一些真正可爱的东西,但是他们的荣誉坚定地指向了那个来自地狱的混蛋操作员,Ralf S.Engelschall,魔法模块本身的作者,我不得不承认,很多事情对我来说仍然是那么多的巫毒

    规则可以在一分钟内运行,但在下一分钟似乎不起作用,浏览器和其他中间网络缓存如何与规则和测试规则交互,这常常令人困惑和恼火。当我觉得有必要把我的思想完全扭曲变形时,我就胡乱地修改修改

    在这一切之后,它确实起了作用,虽然我不打算在短期内参加周末速成班,但我自己也学会了一些小技巧,在这个地方玩网络服务器和网站

    这里的计划是删除一些整洁的东西、示例、已经证明有用的东西,并在各种服务器设置上工作;我的局域网上到处都是Apache,我不断地碰到旧的.htaccess文件,里面塞满了过去的重写实验,这些实验要么有效;我把它们加入了我的名单,或者失败了;我感到惊讶的是,这些天来,我更多地看到了原因

    这里很少有我自己的发明。即使是我自己发现的那些片段已经有了很好的记录,我只是不理解这些文档,或者找不到它们。有时候,仅仅从不同的角度看同一件事就可以让事情变得完全不同,因此,也许这种简单的URL重写可能会有一些用处。当然,我是为自己写的。但我确实为此得到了一些赞扬

    是时候变得活跃起来了,请参见。。 重写规则(.*.htm$1.php

    开始重写..

    Whenever you use mod_rewrite (the part of Apache that does all this magic), you need to do..
    
    每个.htaccess文件只需执行一次:

    Options +FollowSymlinks
    RewriteEngine on
    
    …在任何重写规则之前。注意:+FollowSymLinks必须启用才能使任何规则工作,这是重写引擎的安全要求。通常它是在根目录中启用的,您不必添加它,但这样做并没有什么坏处,我将把它插入到本页的所有示例中,以防万一*

    下一行只是打开该文件夹的重写引擎。如果这个指令在main.htaccess文件中,那么重写引擎理论上是为整个站点启用的,但是在任何地方编写任何重定向之前,总是添加这一行是明智的

    • 虽然不太可能,但您的主机可能在根级别启用了+FollowSymLinks,但不允许在.htaccess中添加它;在这种情况下,添加+FollowSymLinks将破坏您的设置(可能是500个错误),因此只需删除它,您的规则就会正常工作
    重要提示:虽然此页面上的某些指令可能在浏览器中拆分为两行,但在.htaccess文件中,它们必须完全存在于一行中。如果您拖动、选择并复制此页面上的指令,它们应该可以很好地粘贴到任何文本编辑器中

    简单重写

    简单地说,Apache扫描所有传入的URL请求,检查.htaccess文件中的匹配项,并将这些匹配的URL重写为我们指定的内容。像这样的

    对whatever.htm的所有请求都将发送到whatever.php:

    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^(.*)\.htm$ $1.php [NC]
    
    方便任何人从静态htm(您可以使用.html或.htm(.*)、.htm?)更新站点到动态php页面;对旧页面的请求会自动重写为我们的新URL。没有人注意到任何东西,访问者和搜索引擎都可以通过任何方式访问您的内容。保留规则;作为额外的好处,这使我们能够轻松地将php代码及其包含的html结构拆分为两个单独的文件,这是一个好主意;使编辑和更新变得轻而易举。末尾的[NC]部分表示“不区分大小写”或“不区分大小写”;更多关于开关的信息,稍后

    人们可以链接到whater.htm或whater.php,但他们总是在浏览器中获得whater.php,即使whater.htm不存在,这也可以工作!但是我迷路了

    就目前而言,这有点棘手;人们的浏览器地址栏中仍然会有which.htm,并且仍然会将旧的.htm URL作为书签。搜索引擎也会继续将你的链接索引为.htm,一些人甚至认为,从两个不同的地方提供相同的内容可能会让你受到搜索引擎的惩罚。这可能会困扰你,也可能不会困扰你,但如果真的困扰你,mod_rewrite可以发挥更大的魔力

    这将执行“真正的”外部重定向:

    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^(.+)\.htm$ http://xxxx.org/$1.php [R,NC]
    
    不是-
    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^files/([^/]+)/([^/]+).zip /download.php?section=$1&file=$2 [NC]
    
    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^blog/([0-9]+)-([a-z]+) http://corz.org/blog/index.php?archive=$1-$2 [NC]
    
    Escaping:
    
    \char escape that particular char
    
        For instance to specify special characters.. [].()\ etc.
    
    Text:
    
    .             Any single character  (on its own = the entire URI)
    [chars]       Character class: One of following chars
    [^chars]      Character class: None of following chars
    text1|text2   Alternative: text1 or text2 (i.e. "or")
    
        e.g. [^/] matches any character except /
             (foo|bar)\.html matches foo.html and bar.html
    
    Quantifiers:
    
    ? 0 or 1 of the preceding text
    * 0 or N of the preceding text  (hungry)
    + 1 or N of the preceding text
    
        e.g. (.+)\.html? matches foo.htm and foo.html
             (foo)?bar\.html matches bar.html and foobar.html
    
    Grouping:
    
    (text)  Grouping of text
    
        Either to set the borders of an alternative or
        for making backreferences where the nthe group can 
        be used on the target of a RewriteRule with $n
    
        e.g.  ^(.*)\.html foo.php?bar=$1
    
    Anchors:
    
    ^    Start of line anchor
    $    End   of line anchor
    
        An anchor explicitly states that the character right next to it MUST 
        be either the very first character ("^"), or the very last character ("$")
        of the URI string to match against the pattern, e.g.. 
    
        ^foo(.*) matches foo and foobar but not eggfoo
        (.*)l$ matches fool and cool, but not foo