3

I'm trying to figure out how to rewrite urls using apache webserver and php.

The url below is the real nonrewritten url:

http://localhost:1337/rewritetest/index.php?id=12

And I want to reach it by

http://localhost:1337/rewritetest/index/12

My indexfile looks like this:

<?php 
   echo $_GET['id'];
?>

Is this possible? The "new" url doesn't include any parameter names so I guess I have to use an order of parameters instead but I dont know how to reach them in that case.

Below is as far I've come with my rewrite:

RewriteCond %{QUERY_STRING} id=([-a-zA-Z0-9_+]+)  
RewriteRule ^/?index.php$ %1? [R=301,L]  
RewriteRule ^/?([-a-zA-Z0-9_+]+)$ index.php?id=$1 [L]

Anyone have an idea of what I'm doing wrong?

4
  • Where is your .htaccess file located? Commented Sep 27, 2017 at 7:46
  • Sorry, it's located in the same folder as index.php. And it's the only htaccess on the server, and nothing configured on the serverside Commented Sep 27, 2017 at 7:47
  • Is it always supposed to rewrite to index.php (as your code suggests) or should it redirect to whatever basename is specified in the URL (as your URL suggests)? eg. ..../foo/26 to ..../foo.php?id=26? Commented Sep 27, 2017 at 7:50
  • I mainly want to know how to rewrite to one file specific, but if a general solution looks kind of similiar I would be happy for such solution too. I can however work that part out myself if I get help with going in the right direction Commented Sep 27, 2017 at 7:56

1 Answer 1

1

it's located in the same folder as index.php

So, given the .htaccess file is located at /rewritetest/.htaccess (as opposed to the document root ie. /.htaccess) then...

RewriteRule ^/?([-a-zA-Z0-9_+]+)$ index.php?id=$1 [L]

If you request a URL of the form /rewritetest/index/12 then the above RewriteRule pattern won't actually match anything. It tries to match "index/12", but your pattern does not contain a slash so will fail. (Is the + inside the character class intentional?)

Try something like the following instead:

 RewriteRule ^(index)/(\d+)$ $1.php?id=$2 [L]

This obviously specifically matches "index" in the URL. If you are always rewriting to index.php then you don't really need "index" in the URL - unless this means something different? This also assumes that the valuue of the id parameter consists only of digits.

To rewrite the more general .../<controller>/26 to .../<controller>.php?id=26 (as mentioned comments) then try something like:

 RewriteRule ^([\w-]+)/(\d+)$ $1.php?id=$2 [L]

In per-directory .htaccess files the slash prefix is omitted on the URL-path that is matched by the RewriteRule pattern, so /? is not required. The above pattern also matches something for for the id, not anything. So, /index/ would not match.


If this is a new site then the "redirect" (from /index.php?id=12 back to /index/12) is not necessarily required. That's only really required if you are changing the URL structure on an existing site where old URLs already have inbound links and are indexed by search engines. In which case you could do something like the following before the internal rewrite:

RewriteBase /rewritetest/

RewriteRule %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^id=(\d+)
RewriteRule ^(index)\.php$ $1/%1 [R,L]

Or, for a more generic .../<controller>/26 to .../<controller>.php?id=26 (as above) then change the RewriteRule to:

RewriteRule ^([\w-]+)\.php$ $1/%1 [R,L]

The additional check against the REDIRECT_STATUS environment variable is to prevent a rewrite loop after having rewritten the URL to /index.php?id=12 earlier.

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.