Php 重定向和重写url访问

Php 重定向和重写url访问,php,regex,apache,.htaccess,mod-rewrite,Php,Regex,Apache,.htaccess,Mod Rewrite,我想重写一个网址,我不想现有的链接去旧的网页(地址)了 Options +FollowSymlinks RewriteEngine on RewriteRule ^about-us$ /shop/static_page\.php\?static_page_id=3 [NC,L] 这就是我的.htaccess文件目前的样子,它工作得很好——尽管我添加了一个普通的重定向规则,例如: Redirect 301 /shop/static_page.php?static_page_id

我想重写一个网址,我不想现有的链接去旧的网页(地址)了

Options +FollowSymlinks
RewriteEngine on
RewriteRule    ^about-us$    /shop/static_page\.php\?static_page_id=3    [NC,L]
这就是我的.htaccess文件目前的样子,它工作得很好——尽管我添加了一个普通的重定向规则,例如:

Redirect 301 /shop/static_page.php?static_page_id=3    http://example.com/about-us
这是行不通的,好像这条线不存在。有什么想法吗?

2件事:

  • 规则的顺序很重要,所以在内部重写之前,请使用301
  • 不要将
    mod\u别名
    规则与
    mod\u重写
  • 以下代码应该适用于您:

    Options +FollowSymlinks
    RewriteEngine on
    
    # external redirect from actual URL to pretty one
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+shop/static_page\.php\?static_page_id=3[\s&] [NC]
    RewriteRule ^ /about-us? [R=301,L]
    
    # internal forward from pretty URL to actual one
    RewriteRule ^about-us/?$ /shop/static_page\.php?static_page_id=3 [NC,L,QSA]
    
    2件事:

  • 规则的顺序很重要,所以在内部重写之前,请使用301
  • 不要将
    mod\u别名
    规则与
    mod\u重写
  • 以下代码应该适用于您:

    Options +FollowSymlinks
    RewriteEngine on
    
    # external redirect from actual URL to pretty one
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+shop/static_page\.php\?static_page_id=3[\s&] [NC]
    RewriteRule ^ /about-us? [R=301,L]
    
    # internal forward from pretty URL to actual one
    RewriteRule ^about-us/?$ /shop/static_page\.php?static_page_id=3 [NC,L,QSA]
    

    谢谢你的提示,你能发布一个它应该是什么样子的例子吗?非常感谢:)真漂亮。非常感谢@anubhavaThanks提供的提示,您能发布一个它应该是什么样子的示例吗?非常感谢:)真漂亮。非常感谢你@anubhava