'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 :
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?
Solution 1:[1]
Your rules are in the wrong order. Unless /subject/index/sub/ maps to a physical directory then your first rule (that appends the .php extension) will attempt to rewrite the request first and prevent the later rules from being processed.
The second rule will also take priority over the third rule, capturing the trailing slash, so the third rule is never processed.
Since you are rewriting extensionless URLs, you need to make sure that MultiViews is also disabled, otherwise this will take priority and the URL parameters will not be passed to your script.
Try your rules like this instead:
# Disable MultiViews
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
# Internally rewrite to "/subject/index.php?sub=<string>"
RewriteRule ^subject/index/sub/([^/]+)/?$ /subject/index.php?sub=$1 [L]
# To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
No need to repeat the RewriteEngine directive, once you've "turned it on", it stays on.
UPDATE:
https://localhost/subject/content.php?sub=digital-logic&content=this-is-content/i want that url tohttps://localhost/subject/content/sub/digital-logic&content/this-is-content/or any that is possible and i need those both slug for fetching data from different database tables
The same principle applies as above, you just need to be specific in which parts of the URL-path you need to capture in order to create backreferences (ie. $1 and $2) that can be used in the substitution string.
Add the following as the first rule, immediately after the RewriteEngine directive:
# Internally rewrite to "/subject/index.php?sub=<string>"
RewriteRule ^subject/content/sub/([^/&]+)&content/(.+) /subject/content.php?sub=$1&content=$2 [L]
This uses two backreferences, whereas the example above uses just one.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
