dongwh1992 2018-08-22 16:11
浏览 24

难以为Wordpress创建自定义RewriteRules

I am creating a website for a radio station and want to make a section of the site for artists and tracks that are played on the radio. I am currently migrating the code I have written to a Wordpress template, and I'm having difficulty replicating the RewriteRules I have created.

I want a user to be able to type:
/music/<artist name here>/
/music/<artist name here>/<track name here>/

And have it rewrite to:
/music/artist.php?artist=<artist name here>
/music/track.php?artist=<artist name here>&track=<track name here>

e.g.:
/music/Red+Hot+Chili+Peppers/
/music/Red+Hot+Chili+Peppers/Californication/

I was able to achieve this using the following .htaccess file:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^music/([^/]*)/?$ /music/artist.php?artist=$1
RewriteRule ^music/([^/]*)/([^/]{1,})/?$ /music/track.php?artist=$1&track=$2

When I switched to Wordpress, I began by trying replicate the artist page first, and have been unable to do so.

Being that the structure of Wordpress is different from a standard directory/file structure, I decided to try two methods:

  1. Rewrite to /music/ for both of the rules, then decide to include either artist.php or track.php in the "music" page, based on the GET parameters.

  2. Redirect to /artist/ or /track/ and have two different pages instead.

I have tried both methods and neither work.

For method 1, this is the code I added to functions.php (only trying the artist rewrite to begin with):

function custom_rewrite() {
    add_rewrite_rule('music/([^/?]*)/?$', 'music/?artist=$1');
}
add_action('init', 'custom_rewrite');

Upon saving permalink settings (using "day and name"), this created the following .htaccess file:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^music/([^/?]*)/?$ /music/?artist=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Upon navigating to /music/<artist name here>/, depending on different permalink settings, I will either get a 500 error or I will be shown the home page.

For method 2, this is the code I added to functions.php:

function custom_rewrite() {
    add_rewrite_rule('music/([^/]*)/?$', 'artist/?artist=$1');
}
add_action('init', 'custom_rewrite');

Upon saving permalink settings (using "day and name"), this created the following .htaccess file:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^music/([^/]*)/?$ /artist/?artist=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Again, upon navigating to /music/<artist name here>/, depending on different permalink settings, I will either get a 500 error or I will be shown the home page.

Is there something I'm missing in the .htaccess file? Am I somehow conflicting with Wordpress' permalink settings? Is this simply just not possible in Wordpress? Any help would be greatly appreciated.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
    • ¥20 关于URL获取的参数,无法执行二选一查询
    • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
    • ¥15 marlin编译错误,如何解决?
    • ¥15 有偿四位数,节约算法和扫描算法
    • ¥15 VUE项目怎么运行,系统打不开
    • ¥50 pointpillars等目标检测算法怎么融合注意力机制
    • ¥20 Vs code Mac系统 PHP Debug调试环境配置
    • ¥60 大一项目课,微信小程序
    • ¥15 求视频摘要youtube和ovp数据集