John Davidson

How to get the passed variable which then rewritted from .htaccess in php?

0 comments
Message:


In my php code i have passed the variables in url as slug like :


https://localhost/subject/index.php?sub=web-technology

Here sub is the variable name where web-technology is passes as slug from the database, and then the url is rewritten by .htaccess


Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On

# To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^subject/index/sub/(.*) /subject/index.php?sub=$1
RewriteRule ^subject/index/sub/(.*)/ /subject/index.php?sub=$1

From there the url I got is


https://localhost.com/subject/index/sub/web-technology/

or


https://localhost/subject/index/sub/[Any_Value]/

The php code I have in my web is


$subject =mysqli_real_escape_string($conn, $_GET['sub']);

on where I want the slug value in GET variable and match it in database to get the respective data. I cannot get the variable so I cannot retrieve the information from my database.


Can anyone help me with it or can give different ways on how I can make it work properly?



My Answer:
To get the passed variable from the URL that has been rewritten by .htaccess in PHP, you can use the $_GET superglobal array. In this case, since the variable name is "sub", you can access the value of the variable like this:

php
$sub = $_GET['sub'];
echo $sub;


This will output "web-technology" in this case. You can then use this variable in your PHP code as needed.

Rate this post

3 of 5 based on 3946 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us