John Davidson

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


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


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



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:

$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


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