doulachan8217 2014-12-03 12:16
浏览 11
已采纳

mod重写-new.php到/ new

I want to rewrite any page url which contains -new.php to the /new

For example:

example.com/page-new.php

to

example.com/page/new

I am able to remove the ending .php with following rule, but I can't figure out how to match the -new.

RewriteRule ^([^\.]+)$ $1.php [NC,L]
  • 写回答

1条回答 默认 最新

  • dongyinpan9284 2014-12-03 12:36
    关注

    Your rule actually adds .php to the end.

    RewriteRule ^(.*)-new\.php$ $1/new [NC,L]
    

    removes -new.php and changes it to /new


    EDIT:

    After a while of thinking, if you want users to get redirected, use above and add "R=301" to the brackets as third parameter.

    OR, if you actually have links in /page/new schema and files in -new.php schema, then you actually wants to remowe /new and add -new.php. If so, then you asked wrong question. Here you go:

    EDIT 2:

    You also want to redirect /page to page.php. Now you can do this in two ways:

    RewriteRule ^(.*)/new$ $1-new.php [NC,L]
    RewriteRule ^(.*)$ $1.php [NC,L]
    

    OR

    RewriteRule ^(.*)/new$ $1-new [NC]
    RewriteRule ^(.*)$ $1.php [NC,L]
    

    First one are two separate rules, first for /new URLs and then for any other.
    Second code is like "first change /new to -new, and then apply next rule"

    If you want rule to apply only to urls without dots or anything, then replace (.*) with any other rule like for example ([^\.]*)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的