Apache's rewrite module commands not working in .htaccess

Soldato
Joined
25 Feb 2003
Posts
3,263
Location
Stafford (uni)
Trying to run some rewrite module commands on the .htaccess file in the root directory of my web server.

This one is simply to point lyrics/artist/title directories to the php file, I get a 500 internal server error from it:
Code:
RewriteEngine On
RewriteRule ^lyrics/([0-9]+)/([0-9]+) lyrics.php?artist=$1&title=$2





This one is to point the root directory to index.php thus overwriting the auto index, I know I can do this with a simple redirect command but that shows the filename in the URL and I don't want to do that:

Code:
RewriteRule ^/$ index.php
This one just doesn't work and the site just uses the auto index of index.html.
 
Last edited:
Are the artist and title just id numbers then, rather than actual artists and titles?

RewriteRule ^lyrics/([0-9]+)/([0-9]+)$ /lyrics/$1/$2/ [R=301]
RewriteRule ^lyrics/([0-9]+)/([0-9]+)/?$ /lyrics.php?artist=$1&title=$2 [L]

First line just neatens up any links that haven't got a trailing slash.
 
Ah sorry what I meant was:
Code:
RewriteRule ^lyrics/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+) lyrics.php?artist=$1&title=$2

I think thats right, it's artist and title names that will be in there.

edit: Thanks for the solution to the index.

edit2: works now, thanks.
 
Last edited:
You can use

RewriteRule ^lyrics/([^/]+)/([^/]+)$ /lyrics/$1/$2/ [R=301]
RewriteRule ^lyrics/([^/]+)/([^/]+)/?$ /lyrics.php?artist=$1&title=$2 [L]

If there's a unknown string
 
paulsheffII said:
You can use

RewriteRule ^lyrics/([^/]+)/([^/]+)$ /lyrics/$1/$2/ [R=301]
RewriteRule ^lyrics/([^/]+)/([^/]+)/?$ /lyrics.php?artist=$1&title=$2 [L]

If there's a unknown string

Ah ok thanks, that solves the problem of matching spaces in the regEx. Is it safe to accept any user input through the URL though? Or do I need a regEx expression at some point to only match [a-zA-Z0-9_] and spaces?

Ah I think this is what I need:
[a-zA-Z0-9_\s]

edit: Nope that doesn't work, guess i'll just have to go with the match unknown string expression even if it is more unsafe.
 
Last edited:
Yeah I would replace any spaces with hypens, underscores look better I think but there's a common debate saying search engines don't see underscores as spaces.

What might be a good idea is not use the artist and title, and use their id numbers instead, that way all you would need to do is check the variable is numerical only.

RewriteRule ^lyrics/([^/]+)/([0-9]+)/([^/]+)/([0-9]+)$ /lyrics/$1/$2/$3/$4/ [R=301]
RewriteRule ^lyrics/([^/]+)/([0-9]+)/([^/]+)/([0-9]+)/?$ /lyrics.php?artist=$1&artist_id=$2&title=$3&title_id=$4 [L]
 
paulsheffII said:
Yeah I would replace any spaces with hypens, underscores look better I think but there's a common debate saying search engines don't see underscores as spaces.

What might be a good idea is not use the artist and title, and use their id numbers instead, that way all you would need to do is check the variable is numerical only.

Hyphens look neater than underscores IMO: they're more associated with and commonly used in language whereas underscores are less conventional in this sense.

If at all possible aim to have descriptive URIs rather than those based on numerical ID values, it's good practice and kind on your visitors.
 
psyr33n said:
Hyphens look neater than underscores IMO: they're more associated with and commonly used in language whereas underscores are less conventional in this sense.

If at all possible aim to have descriptive URIs rather than those based on numerical ID values, it's good practice and kind on your visitors.

I just think underscores are more out of the way visually, plus I think the fact that hypens can occour in normal text, it might look weird to people to see them used in that way.

The example I gave means you can use both the descriptive elements of the title along with the ID numbers in the address for easy user input checking. A lot of dynamic address follow the format of having descriptive/titles/of/content/with/an/article-id-number.html which does both nicely.
 
Last edited:
Back
Top Bottom