Souvit Singharoy Souvit Singharoy - 2 months ago 6
PHP Question

How to pass a value to any url using htaccess

I want to pass a string (

p=number
) to all the URL in my website

Example:


www.abc.com/p/1/my-filename-1

www.abc.com/p/2/my-new-filename-anything


This is my current code but it is not working:

<IfModule mod_rewrite.c>
Options +FollowSymLinks -Multiviews
RewriteEngine On

# Remove .php file extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]


# For individual page url
#RewriteRule ^p/(.*)\.php $1.php?p=$2

</IfModule>

Answer

Try this rule, here we are getting digit from url and assigning it to the respective file with removed extension.

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)/$ $1.php [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^p/([\d]+)/([\w-]+)$ $2.php?id=$1 [NC,L]