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 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?