duangan6797 2015-04-30 14:54
浏览 197
已采纳

如何使用.htaccess将静态重定向到动态URL(在Wordpress网站上)

I've taken over a former site/domain, and set up a new site using Wordpress. The WP installation rewrites URL's to static ones, as you'd expect it to.

At the same time I want to preserve the former pages, as they have incoming links. I'm not interested in 301'ing them to "new" pages.

The old URL structure is /index.php?id=123, which I suspect is causing the problem with the WP .htaccess file. For reference, this is what it looks like:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

I've tried adding the following:

RewriteRule ^([0-9]+).html index.php?id=$1 [R,L]

Doesn't work. Just redirects to site.com/?id=123 and shows the front page.

I should add that I plan on just adding these new pages as regular static HTML files in the format of 123.html, 321.html etc.

How do I use .htaccess to make this work together with the WP installation and what WP puts into the .htaccess file?

To clarify:

I want to have my 123.html static HTML page be index.php?id=123. When you access index.php?id=123 it should bring up 123.html, but show index.php?id=123 in the address bar. If you access 123.html it should 301 to index.php?id=123.

  • 写回答

1条回答 默认 最新

  • dsjswclzh40259075 2015-04-30 15:49
    关注

    To map an URL with a querystring up to an actual file you'll need to use a RewriteCond to match the querystring itself (as RewriteRule doesn't):

    Something along these lines ought to do it:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    # retrieve X.html when index.php?id=X is requested
    RewriteCond %{REQUEST_FILENAME} index\.php
    RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
    RewriteCond %1.html -F
    RewriteRule .* %1.html? [L]
    
    # standard WordPress routing
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    

    This will first check to see if you've got a request for index.php with a querystring like id=X.

    Then it'll check to see if a file called X.html actually exists; I'm not 100% happy about having to use the more system hungry subrequest file check -F rather than the standard -f but I can't see a way around it in .htaccess in this case.

    If X.html actually exists, it'll fetch that file whilst leaving the URL as index.php?id=X.

    However if that file doesn't exist it'll fall back to standard WordPress no file, no directory routing to index.php

    I'm not a WordPress expert but that should work; I guess the main WordPress controller uses $_SERVER['REQUEST_URI'] to determine the action.


    Note: This won't, however, prevent people from accessing 123.html directly by going to the URL www.site.com/123.html - I kept falling into infinite loops and Apache 500 errors trying to prevent that :|

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

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常