最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

url rewriting - How to rewrite URL with PHP variables with htaccess to a normal looking URL?

programmeradmin2浏览0评论

I want to change an old database website that is not made in WordPress to WordPress. My site uses PHP variables so the URL is originally:

example/index.php?tl=1&pg=2&bs=3&hcbs=4&sc1bs=5&sc2bs=6&sc3bs=7&hc=8&blbs=9

But I could rewrite that to a normal looking URL:

example/1/2/3/4/5/6/7/8/9.php

To do that in the old site I used the following rewrite rules in the .htaccess file:

RewriteEngine on

RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*).php ?tl=$1&pg=$2&bs=$3&hcbs=$4&sc1bs=$5&sc2bs=$6&sc3bs=$7&hc=$8&blbs=$9 [L]
RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*).php ?tl=$1&pg=$2&bs=$3&hcbs=$4&sc1bs=$5&sc2bs=$6&hc=$7&sc3bs=$8 [L]
RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*).php ?tl=$1&pg=$2&bs=$3&hcbs=$4&sc1bs=$5&hc=$6&sc2bs=$7 [L]
RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/(.*).php ?tl=$1&pg=$2&bs=$3&hcbs=$4&hc=$5&sc1bs=$6 [L]
RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*).php ?tl=$1&pg=$2&bs=$3&hc=$4&hcbs=$5 [L]
RewriteRule ^(.*)/(.*)/(.*).php ?tl=$1&pg=$2&bs=$3 [L]

But now I am trying to do so in WordPress. WordPress already has some preinstalled rewrite rules:

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

But when I try to combine the both in any way the complete WordPress site stops working. Does anyone have an idea how to solve this?

I want to change an old database website that is not made in WordPress to WordPress. My site uses PHP variables so the URL is originally:

example/index.php?tl=1&pg=2&bs=3&hcbs=4&sc1bs=5&sc2bs=6&sc3bs=7&hc=8&blbs=9

But I could rewrite that to a normal looking URL:

example/1/2/3/4/5/6/7/8/9.php

To do that in the old site I used the following rewrite rules in the .htaccess file:

RewriteEngine on

RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*).php ?tl=$1&pg=$2&bs=$3&hcbs=$4&sc1bs=$5&sc2bs=$6&sc3bs=$7&hc=$8&blbs=$9 [L]
RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*).php ?tl=$1&pg=$2&bs=$3&hcbs=$4&sc1bs=$5&sc2bs=$6&hc=$7&sc3bs=$8 [L]
RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*).php ?tl=$1&pg=$2&bs=$3&hcbs=$4&sc1bs=$5&hc=$6&sc2bs=$7 [L]
RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/(.*).php ?tl=$1&pg=$2&bs=$3&hcbs=$4&hc=$5&sc1bs=$6 [L]
RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*).php ?tl=$1&pg=$2&bs=$3&hc=$4&hcbs=$5 [L]
RewriteRule ^(.*)/(.*)/(.*).php ?tl=$1&pg=$2&bs=$3 [L]

But now I am trying to do so in WordPress. WordPress already has some preinstalled rewrite rules:

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

But when I try to combine the both in any way the complete WordPress site stops working. Does anyone have an idea how to solve this?

Share Improve this question edited Apr 9, 2020 at 8:32 MrWhite 3,8911 gold badge20 silver badges23 bronze badges asked Apr 9, 2020 at 6:27 Hanno KHanno K 112 bronze badges 3
  • you cannot use the old rewrite rules because the WordPress have its own rewrite system (called permalink) inside using regular expression. You can see there is only index.php because when WP receive a website with such as "?query=something", it will turn to pretty url(if permalink is on) if it is defined or it will give you 404. If you want to migrate the old query strategy, you will have to use WP builtin tools to accomplish. If you would like to do so. I suggest you to try a little more of the permalink and observe the difference if it is turn on or off for a test post to understand first. – 西門 正 Code Guy - JingCodeGuy Commented Apr 9, 2020 at 7:22
  • Actually website?tl=1 is working. So it does not bring me a 404 error. I only want to make it look like website/1/ and in a way that my php code can still get the variable tl. If you know how tell me – Hanno K Commented Apr 9, 2020 at 7:25
  • I have added an example on how to do a simple rewrite, but I am not sure what's your tl parameter for. If you are sure tl is working then I think the examples works for you. – 西門 正 Code Guy - JingCodeGuy Commented Apr 9, 2020 at 8:11
Add a comment  | 

1 Answer 1

Reset to default 1

Here is an example of adding custom rewrite rule to WordPress. For instance, you have a link http://website/1/ that would like WordPress interprets internally as http://website?tl=1

But for tl you have mentioned, I am not sure what is your settings. So I assume tl is working in your system. Because WP default parameter is p for page id

You may try to use the following filter in your theme functions.php or plugin

  • The following only works if permalinks is turned on. Otherwise, no effect. It means in settings -> permalinks, if you choose Plain, then WordPress will show the query without pretty URL. It is regarded as turned off. Otherwise, if you choose other options like Post Name then WordPress will accept Post Name as URL such as https://examples/sample-post/

So, if it is being enabled(or turned on). Then the the rewrite system will be used. And the following filter works with WordPress rewrite system.

Sometimes we use some other synonym like Enable permalinks, Disable permalinks. It also means the same things.

Here, assume to place in functions.php

add_action( 'init', 'q363603_add_custom_rewrites' );
function q363603_add_custom_rewrites() {
        // take numbers to the query tl=numbers
        add_rewrite_rule('([0-9])+/?$', 'index.php?tl=$matches[1]', 'top');
}

add_filter('query_vars', 'q363603_add_custom_queryvars' );
function q363603_add_custom_queryvars( $qvars ) {
    // ask WordPress to allow this query parameter
    $qvars[] = 'tl';
    return $qvars;
}

edited: add permalinks turning on/off explanation

发布评论

评论列表(0)

  1. 暂无评论