A basic rewrite is giving me a 404 page

Hey all,

I’m new to rewriting.
I’m trying to get a url to split into parameters to pass to php.

The rewrite looks like this.

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.*)$ index.php?id=$l [QSA, L]  

But when I browse to www.site.com/testing I get an error.

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webadmin@kundenserver.de and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

This is a shared hosting package. I have WordPress working on one of the other domains perfectly. This uses rewrites so I’m fairly sure it’s enabled.

Any help would be great, thanks.

Hey,

I have got a bit further but now I have a 404 page rather than an error 500.

My modified htaccess is


AddType x-mapp-php5 .php

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^v/([^/]*)$ index.php?id=$1 [L]

Let’s start really basic, so we can get an idea of where you are and where you want to be.

First give me an example of the URL as it exists today, then show me what you want it to look like.

Once we know those two items, we can get you going in the right direction.

Hey,

Thanks for that

I have been tweaking all day and at the moment I have a page set up at

www.barrasweb.co.uk/hello

Which redirects to barrasweb.co.uk/index.php?id=

I’d prefer it to rewrite rather than redirect but its a start.

the htaccess file is as below. There is a load of junk and comments in there where I have been working.


AddType x-mapp-php5 .php

Options +FollowSymLinks
RewriteEngine On
#RewriteBase /

#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-l

#RewriteRule ^(.*)$ index.php?id=$l [QSA, L]
#RewriteRule ^([a-z0-9\\-]+)$ index.php?id=$l [QSA, L]
##RewriteRule ^v/([^/]*)$ index.php?id=$1 [L]
##RewriteRule ^v/hello.html landed.html [L]
### WORKING RewriteRule ^hello\\.html$ /landed.html [R,NC,L]

### STILL WORKS RewriteRule ^hello$ /index.php?id=$1 [R,NC,L]

RewriteRule ^hello$ /index.php?id=$1 [R,NC,L]

The last line in there seems to work but obviously it’s only looking for a static hello page.

In the end I’d like

/folder/page

to redwrite to

/index.php?id=folder/page

Thanks in advance.

Ok, still tinkering.

I have just changed the htaccess file to read


RewriteEngine On
RewriteBase /
RewriteRule ^(index\\.php)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php?id=$1 [L]

This seems to work but it’s not passing anything to the GET super global.

Thanks again

Okay, so you basically want to do a catch-all and send it to index.php under the variable id.

RewriteEngine On
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?id=$1 [L]

I typically don’t recommend catch-alls, but I also understand why one may want to control it pragmatically where they feel they have better control over the processing versus creating different handler files and having apache capture the data and give it to the appropriate handler. Two different methods that ultimately achieve the same end goal.

The above should capture the information and pass it into the id variable.

Absolutely fantastic, thanks for that.

Works perfectly. I just wish I knew what was going on.

I think I get most of it except the two RewriteRules. I understand the concept of the second one but I’m not sure what the first one is doing.

Well, the first one simply states, if the request is for index.php leave it alone and just let it process index.php.

The second says, capture everything, store it in $1 and pass to index.php in the variable id

An explanation for both of you:

  1. It’s ALWAYS dangerous to use (.) BECAUSE it does capture EVERYTHING (or NOTHING) so any hiccup in your website will be caused by this inappropriate use of (.). This is explained in my signature’s tutorial as well as ways to correctly specify what you WANT to match (‘hello’ in this case).

  2. The {REQUEST_FILENAME} lines were looking to NOT match a file (!-f), directory (!-d) or symbolic link (!-l). I have no idea why you’d want to involve a symbolic link but … well, the other two are fine for the generic case (where you have no idea what the URI will be).

  3. The {REQUEST_URI} used to exclude index\.php was used to escape from the infinite loop of the :kaioken: EVERYTHING :kaioken: atom, (.). Of course, as you add images and scripts, they’ll have to be added to the list of excluded files (surprise! That’s why you should NEVER use (.) unless you actually know what you’re doing.

  4. Kudos to cp for showing you how to create Apache variables (using the parenthetical in a mod_rewrite statement). More on that in my signature’s tutorial, too.

Regards,

DK