I cannot use $_Get when I add my .htaccess

Johan Alden Source

I have a website that works fantastic. I'd like to organize it a little better so instead of loading every file by typing it in the url, i'll just make a index file that can include the files by a get method.

$key = $_GET['KEY'];
if ($key == 'signup'){
include "forming/signup.php";
}

To make the url a little cleaner I added this in the .htaccess file

RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ index.php?KEY=$1
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?KEY=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Which turned

localhost:8888/index?KEY=signup

into

localhost:8888/signup

Now the only problem is that when I use the $_GET, it simply doesn't notice it. I noticed it by a redirect (Programmed in If statement) so I tested it by making a file and with this code

<?php
$test = $_GET['id];
echo "<h1>" . $test . "</h1>";

And it didn't work, like expected.

I don't know why $_GET stopped working after I used that .htaccess code. (I'm a novice at .htaccess)

I would truly appreciate if anyone could tell me another way to cleanse the url and still have the $_GET working. (Or just if you see any error in the .htaccess)

UPDATE

I'd like to clarify. Essentially I want this

localhost:8888/index.php?KEY=post&id=1

to

localhost:8888/post/1

(But for every post, not just nr 1)

php.htaccess

Answers

answered 2 months ago Lewis Donovan #1

As PHP is a server-side language, $_GET only works on what's actually in the URL string at the time the request is sent to the server. If you purely want to alter what's visible in the address bar in order to achieve a cleaner look, you'll need to use a server-side language.

Luckily you can do this with Javascript pushState. There's an answer here that covers it in all possible detail.

answered 2 months ago Wahid Lahouiter #2

If you want my advice, follow these steps to get the best site :)

1 -> First, create folder in your public_html , name it : "auth" for example

in this folder you can upload you files like "signin.php" , "signup.php" , "forget_password.php"

2 -> in your htaccess file , put this :

I've written this file for you, he is ready :)

    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            deny from 10.0.0.182
            AddDefaultCharset utf-8
            Options -MultiViews
            Options -Indexes
            DirectoryIndex index.php
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_FILENAME}.php -f
            RewriteRule ^(.*)$ $1.php
            RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
            RewriteRule ^(.+)\.php$ /$1 [R=301,L]
            #REDIRECTS HERE LIKE REF SYSTEM
            RewriteRule ^ref/(.*)$ auth/signup?ref=$1 [L]
       </IfModule>  
   </IfModule>  

3 -> Your links now is :

NOTE : in ref system , you can get username value from $_GET['ref']

comments powered by Disqus