0

I'm having this issue which I can't figure out, I'd like to use mod_rewrite to do the following:

http://localhost/The+Beatles

into

http://localhost/artist.php?a=The+Beatles

I've tried following some examples on here and other sites and tayloring it to my needs I'm come up with the following but it's not working:

RewriteEngine on
RewriteRule ^(.*) artist.php?a=$1

It seems to set the a variable to artist.php which I can't seem to figure out? Hope someone can help.

Dave

4
  • What is it that's not working exactly? Commented Aug 18, 2011 at 23:22
  • It's not resolving the url i've tried RewriteRule ^([a-z]/+)/?$ artist.php?a=$1 which will work if it's a single string I think it has a problem with the + maybe in between the words? Commented Aug 18, 2011 at 23:28
  • (1) I would better say RewriteRule ^(.*) artist.php?a=$1 [L] (2) mod_rewrite is usually not easy to setup correctly from fist go, so use RewriteLog /tmp/rewrite.log + RewriteLogLevel 2 so see what's going on. (3) If that does not help, tell us do you put the rules to server config or to .htaccess? Commented Aug 18, 2011 at 23:37
  • thanks for your help dma_k Phil fixed below Commented Aug 19, 2011 at 0:09

1 Answer 1

1

You could try this set of rules (adapted from a vanilla Zend Framework project)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteRule ^(.*)$ artist.php?a=$1 [L,QSA]

This will rewrite requests for non-existent files to your artists.php script whilst leaving normal requests for images, stylesheets, JavaScript, etc alone.

In your artist.php script, you simply access the original request via the $_GET['a'] variable.

Sign up to request clarification or add additional context in comments.

6 Comments

Hey Phill, tried this however I couldn't get it to work without 500 itnernal errors, removed everything but the last line and the [L, QSA] and it works and displays my page but it says $_GET['a'] = artist.php ?? not sure whats going on here :( thanks for your time
logs show [Thu Aug 18 23:48:34 2011] [alert] [client localhost] /var/www/.htaccess: RewriteLog not allowed here
@Dave What did it say before you added RewriteLog? Was it still erroring? FYI, RewriteLog is only allowed in server or virtualhost contexts
Got it! with the above code you gave me I get this : [Thu Aug 18 23:57:01 2011] [alert] [client localhost] /var/www/.htaccess: RewriteRule: bad flag delimiters
@Dave Try removing the space between L, and QSA (updated my answer)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.