0

I think I just need a second pair of eyes for this as I can't see why i'm getting a server error.

RewriteEngine On
RewriteRule ^Gig/([a-zA-Z0-9_-]+)$ gig.php
RewriteRule ^Gig/([a-zA-Z0-9_-]+)/$ gig.php
#allow non caps
RewriteRule ^gig/([a-zA-Z0-9_-]+)$ gig.php
RewriteRule ^gig/([a-zA-Z0-9_-]+)/$ gig.php

Edit:

I have now viewed the log and the reason is there are far too many internal redirects. I myself am not too competent at mod_rewrites etc so please have a look.

#redirect so home page shows /Home
Redirect /home.php http://localhost/Home

#add php extension
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

Redirect /home.php http://localhost/Home

# redirect to .php-less link if requested directly
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.*)\.php /$1 [R=301,L]

#redirect www to non-www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.^([a-zA-Z0-9_-]+)$ [NC]
RewriteRule ^(.*)$ http://^([a-zA-Z0-9_-]+)$1 [L,R=301]

#remove trailing slash
RewriteEngine on 
RewriteRule ^(.*)/$ /$1 [L,R=301]

#allow artistprofile nice url
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\-]+)/?$ artist_profile.php

#info nice url
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\-]+)/[Aa]bout/?$ artist_about.php

#gigs nice url
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\-]+)/[Gg]igs/?$ artist_gigs.php

#tracks nice url
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\-]+)/[Tt]racks/?$ artist_tracks.php

#gig nice url
RewriteEngine On
RewriteRule ^[Gg]ig/([a-zA-Z0-9_\-]+)/?$ gig.php

That's all the rewrites in the .htaccess file

Edit: The problem is the adding PHP extension part

#add php extension
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

I think its due to the ([a-zA-Z0-9_-]+) being after the file name.

Jacob Windsor
  • 6,750
  • 6
  • 33
  • 49

1 Answers1

1

You need to escape your - characters when they're inside brackets. You can also shorten your code to the following:

RewriteEngine On
RewriteRule ^[Gg]ig/([a-zA-Z0-9_\-]+)/?$ gig.php
# Escape it!                    ^^

Why can we shorten it like this?

  • [Gg] means "The character G or g"
  • /? means "/ repeated 0 or 1 time"
h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
  • Thanks for showing me how to shorten it. I am still getting the same error. I can't look at the log at the moment, trying to sort that now. If this helps it works if you just type in `/gig` rather than `gig/123` – Jacob Windsor May 29 '13 at 11:27
  • @nbs189 I've just tried myself, and the code works fine. Are you sure that you have a `gig.php` file in your root? Are you getting a `404` error or a `500` error? – h2ooooooo May 29 '13 at 11:36
  • I've managed to look at the log and found the problem. Please see my edit – Jacob Windsor May 29 '13 at 11:46
  • Unless Apache uses a RegEx engine that differs from others in this point, the `-` does _not_ have to be escaped if it is at the beginning or end of a character class. – CBroe May 29 '13 at 11:49
  • @h2ooooooo I've found the specific error in the rewrites. I am not too good at this kind of thing so am having difficulty solving it. If you could have a quick look it'd be greatly appreciated – Jacob Windsor May 29 '13 at 11:56
  • @nbs189 What are you trying to match with `RewriteRule ^(.*)$ http://^([a-zA-Z0-9_-]+)$1 [L,R=301]` ? This means "*any character repeated 0 or more times*" so it will match everything. – h2ooooooo May 29 '13 at 12:10
  • @nbs189 You might want to take a look at [**this**](http://stackoverflow.com/a/1270281/247893). – h2ooooooo May 29 '13 at 12:12
  • It does match everything, I will change it to the site url when it goes lives its just for the mean time – Jacob Windsor May 29 '13 at 12:22
  • @nbs189 But that's the problem - you can't use a regex in the destination URL of a `RewriteRule`. How would it know where to put it? – h2ooooooo May 29 '13 at 12:30
  • @h2ooooooo The `www` rewrite is not causing this problem. I shall look into that later, thanks for pointing it out – Jacob Windsor May 29 '13 at 12:38