0

I would like to rewrite www.mysite.com/a/b/slug to www.mysite.com/a/b/index.php?id=slug.

I am trying to capture the last segment of the path i.e. slug and use it in the query string -- similar to this example.

So I have the following lines in my .htaccess sitting in my public_html folder:

RewriteEngine on
RewriteRule ^a/b/([^/]+) a/b/index.php?id=$1 [L]

In my a/b/index.php, when I go to www.site.com/a/b/slug, $_GET['id'] returns index.php opposed to slug. I am not sure why.

How can I capture the last segment and use it in the query string?

Mikey
  • 6,728
  • 4
  • 22
  • 45
  • 1
    do you need the URL to look like `www.mysite.com/a/b/index.php?id=slug` I would leave the URL alone, and just use `$_SERVER` variables and maybe RegEX to get your 'slug'. See this answer https://stackoverflow.com/a/3429268/3790921 – Chad Dec 01 '17 at 17:08
  • @Chad Thanks. I would consider that option but I found the solution. – Mikey Dec 01 '17 at 17:25

1 Answers1

3

You need to add rewrite conditions in your .htaccess - at the moment, it is attempting to rewrite /a/b/index.php as well!

Try adding RewriteCond %{REQUEST_FILENAME} !-f and RewriteCond %{REQUEST_FILENAME} !-d

Luke
  • 1,060
  • 8
  • 19