0

I've never needed to use a .htaccess before and I'm fairly new to coding I'm trying to get localhost/index.php?id=123456789 to be passed as localhost/123456789/ but I just cant get the HTaccess right, i've tried everything I could find from prevoius posts here, haha!

My current HTACCESS looks like this, however it doesnt do what I want.

RewriteEngine On
RewriteRule ^id/(\d+)$ /index.php?id=$1 [NC,QSA,L] 

1 Answers1

0

You can do it with mod_rewrite in .htaccess however I'm not sure from your question which direction you want it to be passed to.

If you want people to type the php script in the URL bar you want:

RewriteEngine on
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^index.php$ %1/

Or if you want people to enter the "pretty" version but for your server to load the PHP script you need:

RewriteEngine on
RewriteRule ^([0-9]+)/$ index.php?id=$1&%{QUERY_STRING}

For both ways and 301 redirect to pretty URL:

RewriteEngine on
RewriteRule ^([0-9]+)/$ index.php?id=$1&%{QUERY_STRING} [L]

RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^index.php$ %1/ [L,R=301]
Scottie
  • 99
  • 2
  • Hey, thanks for responding! I'd need it to go both ways, so if someone wanted they could go "example.com/12345/" or so that my site can process "example.com/profile.php?id=12345" in a GET form I have. –  May 10 '16 at 01:04
  • If you use the second one you can get pretty URLs. The old URLs with php script in them will still work but you might want to try redirecting them with the second two lines of the first one so that it only shows the pretty URLs (you should add some tags to stop prevent collision). I will edit my answer with a full solution. – Scottie May 10 '16 at 01:11
  • Alright, I see, cheers, however, any reason my CSS is now not working? And when I use my GET form it still displays as ?id=12345 not /12345/ :S that might just be me though haha –  May 10 '16 at 01:13
  • 1
    If the URL of your CSS matches the pattern then it could redirect that too – Scottie May 10 '16 at 01:16
  • Just seen your bothways one, looks good however any reason it's including the entire path? EG, It now looks like "localhost/C:/Xampp/htdocs/12345/?id=12345" –  May 10 '16 at 01:25