Search code examples
apache.htaccessmod-rewriteurl-rewritingfriendly-url

Trying to debug .htaccess file


I am getting started with the mod_rewrite function for my PHP webpage

I was superexcited to have created my first rewrite rule, but when I added to my .htaccess file, I got error 500 for all webpages (not only the one I am trying to redirect).

Here is my file:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} ^([^/]*)/(([^/]*)/)*$
RewriteCond %{REQUEST_FILENAME}index.php !-f
RewriteCond %{REQUEST_FILENAME}index.html !-f
RewriteCond %{REQUEST_FILENAME}index.htm !-f
RewriteRule ^/([a-zA-Z\-]+)\/([a-zA-Z\-]+)\/([0-9]+)$ ficha_jugador.php?idjugador=$3 [L]
RewriteRule (.*) /index.html [L]

My new line is 8th, the rest came by default in my webserver

I tried something much easier for line 8th just to test

RewriteRule ^writetest\.html http://www.after5pc.net [QSA,L]

But it also ended up in 500 error, so it does not seem a syntax problem

Eventually, what I want to do is to change (slug my URLs)

http://cadistas1910.com/ficha_jugador.php?idjugador=1304
by
http://cadistas1910.com/alvaro-garcia-canto/alvaro-garcia/1304

Any help you guys can provide? Thanks a lot!


Solution

  • I believe last line of your .htaccess file could be the culprit, could you please try following once.

    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^([a-zA-Z\-]+)\/([a-zA-Z\-]+)\/([0-9]+)$ ficha_jugador.php?idjugador=$3 [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ index.html [L]
    

    I have added a condition there to rewrite to index.html if and only if Uri has non existing directory/file, why because it will create an infinite loop without a condition, since you are rewriting everything to index.html and everything(by regex) matched index.html also, hence loop and 500 internal error you are getting.

    Also your rule to rewrite from http://localhost:80/alvaro-garcia-canto/alvaro-garcia/1304 TO http://localhost:80/ficha_jugador.php?idjugador=1304 have been changed into a single line above.

    NOTE: To fix this either make your links absolute or use base tag. In the header of your webpage add this <base href="/"> so that your relative links can load from the correct location.