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:
Rewrite to
/music/
for both of the rules, then decide to include eitherartist.php
ortrack.php
in the "music" page, based on the GET parameters.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.