0

I am trying to get a mod rewrite script to work, but for some reason I cannot seem to get things functioning properly. I know that mod_rewrite is working, because if I run the following test it redirects correctly:

RewriteEngine on
RewriteRule ^oranges.html$ apples.html

However, if I try and push all queries to an index file or something to process using php then it does not work:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .? index.php [L]

What I am trying to achieve is to have a database table with two columns, one with an ID and the other with a rewrite URL. So what I mean by this is:

ID | URL
16 | nice-url-1
17 | nice-url-2

So the actual URL is /index.php?id=16, but I want there to be a table lookup and there to be a redirect to /nice-url-1. Now I would have thought I would go with the second rewrite conditions, but the problem is it just does not redirect.

I am running Centos 6 with Apache 2.2.15, so I am wondering if {REQUEST_FILENAME} is just not supported. I did try adding DOCUMENT_ROOT, but that made absolutely no difference.

Any help most appreciated.

2
  • That RewriteCond/RewriteRule block you've probably seen elsewhere is just a dated detour around using FallBackResource. Commented Oct 3, 2015 at 17:59
  • What you're trying to achieve is called URL slugs. Research more on them. Commented Oct 3, 2015 at 18:01

1 Answer 1

1

You want to add an other element to your rule, to actually pass the whole url, QSA. This will append the existing query string to the url.

RewriteRule ^ index.php [L,QSA]

I also normally use the single ^ to match any url possible.

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

Comments

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.