how write a rule to Hide url parameter using htaccess - php

i am trying to rewrite URL using htaccess.
i need
1> temp.example.com/save.php?item=xxxxx&id=xxx&type=1
2> temp.example.com/save.php?item=xxxxx&id=xxx&type=2
to be like
1> temp.example.com/save/xxxxx/xxx
2> temp.example.com/save/xxxxx/xxx
have to hide last parameter .
my htaccess has
RewriteCond %{THE_REQUEST} \s/save\.php\?item=([_0-9a-zA-Z-]+)\s&id=([_0-9a- zA-Z-]+)\s&type=([_0-9a-zA-Z-]+)\s [NC]
RewriteRule ^temp/save/%1/%2/%3? [R=301,L]
RewriteRule ^temp/save/([_0-9a-zA-Z-]+)$/([_0-9a-zA-Z-]+)$/([_0-9a-zA-Z-]+)$ /save.php?item=$1&id=$2&type=$3 [L]
Thanks

You can use these rules in root .htaccess:
RewriteCond %{THE_REQUEST} /save\.php\?item=([^\s&]+)&id=([^\s&]+)&type=([^\s&]+)\s [NC]
RewriteRule ^ save/%1/%2/%3? [R=302,L]
RewriteRule ^save/([\w-]+)/([\w-]+)/([\w-]+)/?$ save.php?item=$1&id=$2&type=$3 [L,QSA,NC]

RewriteEngine On
RewriteRule ^save.php/([a-zA-Z0-9_-]+)/(.)/(.)$ /save.php?id=$1&order_id=$2&page_no=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
now you can access three parameter by pass like
temp.example.com/save/par1/par2

Related

Htaccess redirect url path different path

I want to masking url with htaccess. I tried a few method but didn't work.
I used this code
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
RewriteRule ^ index.php
RewriteCond %{HTTP_HOST} ^em.example.com/lib/view/map$ [NC]
RewriteRule ^pathurl/(.*)$ /em.example.com/map/$1 [R=301,NC,L]
Url is
https://em.example.com/lib/view/map.php
I want to redirect to
https://em.example.com/map
Try
RewriteRule ^/lib/view/(.*)$ $1 [NC]
RewriteRule ^(.*).php$ $1 [R=301,NC,L]
First rule removes lib/view/ , Second rule removes .php

How to define base URL in .htaccess

I am redirecting
https://www.example.com/inter.php?pid=<a number>&title=<the page title>
to
https://www.example.com/<a number>/<the-page-title>
Now I want to move the website to a sub folder so the redirection will be
https://www,example.com/folder/inter.php?pid=<a number>&title=<the page title>
to
https://www.example.com/folder/<a number>/<the-page-title>
I am using the below .htaccess code
RewriteEngine On
RewriteCond %{THE_REQUEST} /inter\.php\?pid=([^&\s]+)&title=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,NE,L]
RewriteRule ^(\S+)\s+(.*)$ $1-$2 [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^(\S+)$ /$1 [NE,R=302,L]
RewriteRule ^([^/]+)/([^/]+)/?$ inter.php?pid=$1&title=$2 [L,QSA]
Please Suggest.
thanks!
If you are using .htaccess in root directory you can add /foldername
RewriteCond %{THE_REQUEST} /folder/inter\.php\?pid=([^&\s]+)&title=([^\s&]+) [NC]
RewriteRule ^ /foldername/%1/%2? [L,R]
To both rules
RewriteRule ^folder/([^/]+)/([^/]+)/?$ inter.php?pid=$1&title=$2 [L,QSA]
Please try above rules it I didn't tried it for now.

.htaccess not converting the dirty url into clean url

I am having an URL named www.example.com/print?id=adder
I want it to look like www.example.com/print/adder
I am using the following .htacces code
RewriteEngine on
# redirect all requests except only POST
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(?:php?)[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteRule ^print/([a-z]+) print.php?id=$1 [NC,L]
But still, i am having the URL like www.example.com/print?id=adder.
I am using wamp 3.0.6(Latest Version), PHP 7.0,10 & Apache 2.4.23. Why am i facing this problem. Why isn't the code working?
To redirect your url to cleaner version, put this above your last RewriteRule
RewriteCond %{THE_REQUEST} /print/?(?:\.php)?\?id=([^\s&]+)
RewriteRule ^ /print/%1? [L,R]

PHP htaccess rewrite rule to same page with different parameters

I'm using the following htaccess rules. The first one is working fine. The second rule is not working. The rule I have written for the second one is taking to the first one.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /Burjalif/
RewriteCond %{THE_REQUEST} /Products(?:\.php)?\?MC=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{THE_REQUEST} /Products(?:\.php)?\?MC=([^&]+)&SC=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=302,L]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# PHP hiding rule
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^([^/]+)$ Products.php?MC=$1 [L,NC,QSA]
RewriteRule ^([^/]+)/([^/]+)$ Products.php?MC=$1&SC=$2 [L,NC,QSA]
Please help me to fix this.
Change the order of the rules.
Its entering in the first rule every time.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /Burjalif/
RewriteCond %{THE_REQUEST} /Products(?:\.php)?\?MC=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{THE_REQUEST} /Products(?:\.php)?\?MC=([^&]+)&SC=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=302,L]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# PHP hiding rule
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)/([^/]+)$ Products.php?MC=$1&SC=$2 [L,NC,QSA]
RewriteRule ^([^/]+)$ Products.php?MC=$1 [L,NC,QSA]
RewriteRule ^(.*)$ $1.php [L]
Change your first rule to this:
RewriteCond %{THE_REQUEST} /Products(?:\.php)?\?MC=([^\s&]+)\sHTTP [NC]
RewriteRule ^ %1? [R=302,NE,L]
This will ensure that you're matching only one parameter in query string.

Remove php extension from URL - except one and only one URL using htaccess

I'm using this code to remove the .php from the end of my URL's making them more SEF.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/(.+)\.php[^\s]* [NC]
RewriteRule ^ /%1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [QSA,NC,L]
This is generic and works perfectly for every UR but I would like to know how to create an exception for one and only one URL, ie:
http://www.domain.com/services to http://www.domain.com/services.php
without influencing all the others. Is this possible?
I've tried a simples Redirect but entered a loop.
! EDITED !
I failed to mention I have also these rules:
Add www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Remove index (the extension .php from index has been trimmed already)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index/?([^\ \?]*) [NC]
RewriteRule ^ %1/%2 [R=301,L]
You should :
add a RewriteCond, to disabled services.php => services
add another rule, to enforce services => services.php, as the first rule doesn't prevent user to manually use services url
Full .htaccess
# domain.tld > www.domain.tld (visible)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# services > services.php (visible)
RewriteRule ^services/?$ services.php [R=301,QSA,L]
# filename > filename.php (transparent)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [QSA,NC,L]
# filename.php > filename (visible, exception for services.php)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/(.+)\.php[^\s]* [NC]
RewriteCond %{REQUEST_FILENAME} !services\.php
RewriteRule ^ /%1 [R=301,NE,L]
# index > /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index/?([^\ \?]*) [NC]
RewriteRule ^ %1/%2 [R=301,L]
I would believe just a small change in your .php removal rule should do the job:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/(.+?)\.php[\s?] [NC]
RewriteRule !^services\.php$ /%1 [R=301,NE,NC,L]
Can you try this,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
You can add one rule for each php file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^services /services.php [L]

Categories