Rewrite Rule in htaccess convert query string into slashes php - php

OK I've tried multiple solutions but still can't achieve it. so what I need to do is
mydomain.com/search?q=product&l=london
to
mydomain.com/search/product/london
Basically I'm using rewrite rules to hide .php extension by following this code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-
RewriteRule ^([^\.]+)$ $1.php [NC,L]
So now, search.php converts into search and when user searches. it redirects to trailing slash instead of query string
Here what I've used so far
RewriteCond %{THE_REQUEST} /search/?q=([^&]+)&l=([^\s]+) [NC]
RewriteRule ^ /%1/%2? [NC,L,R]
#skip the rule if the request is for dir
RewriteCond %{REQUEST_FILENAME} !-d
#skip the rule if the request is for file
RewriteCond %{REQUEST_FILENAME} !-f
#rewrite any other request to "/packagemenu.php?"
RewriteRule ^([^/]+)/([^/]+)/?$ /search?q=$1&l=$2 [NC,L]
AND
RewriteBase /search/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?q=([^\s&]+)&l=([^\s&]+) [NC]
RewriteRule ^ search/%1/%2/? [R=302,L,NE]
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?q=([^\s&]+)\s [NC]
RewriteRule ^ search/%1/? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# internal forward from pretty URL to actual one
RewriteRule ^search/([^/]+)/?$ search?q=$1 [NC,L,QSA]
RewriteRule ^search/([^/]+)/([^/]+)/?$ search?q=$1&l=$2 [NC,L,QSA]
But both of them doesn't work. I don't have so much knowledge about so please help me out.

Have your site root .htaccess as this:
Options -MultiViews
RewriteEngine On
RewriteBase /jobportal/
# To externally redirect /search.php?q=product&l=london to /search/query/london/
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?q=([^\s&]+)&l=([^\s&]+)\s [NC]
RewriteRule ^ search/%1/%2/? [R=301,L,NE]
# To externally redirect /search.php?q=product to /search/query/
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?q=([^\s&]+)\s [NC]
RewriteRule ^ search/%1/? [R=301,L,NE]
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php\s [NC]
RewriteRule ^ %1 [R=301,NE,L]
# ignore all rules below for real files/directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# internal forward from /search/product/ to search.php?q=product
RewriteRule ^search/([^/]+)/?$ search.php?q=$1 [NC,L,QSA]
# internal forward from /search/product/london/ to search.php?q=product&l=london
RewriteRule ^search/([^/]+)/([^/]+)/?$ search.php?q=$1&l=$2 [NC,L,QSA]
# internal forward from /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

You can achieve the URL mydomain.com/search/product/london by using the following rule in your .htaccess.
RewriteEngine On
RewriteRule ^search/([^/]*)/([^/]*)$ /search?q=$1&l=$2 [L]
Just make sure you clear your cache before testing this.

Try as below,
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/search$
RewriteCond %{QUERY_STRING} ^q=([^\/]+)&l=([^\/]+)$
RewriteRule ^ search/%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Rewriterule ^search/([^\/]+)/([^\/]+)$ search.php?q=$1&l=$2 [QSA,L]

Related

Redirect users with friendly urls htaccess php

I need a hand to redirect and write nice urls for my product requests.
So for any request like
https://www.domain.com.au/pages/product.php?product=PRODUCTNAME
Redirect and rewrite to
https://www.domain.com.au/pages/product/PRODUCTNAME
I actually have:
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{SERVER_PORT} 80
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=301,NE,L]
# End of Apache Rewrite Rules
So it goes to:
https://www.domain.com.au/pages/product/?product=PRODUCTNAME
That, for pages like about.php etc it's fine as you may realize.
Thanks ahead!
You can use the following to shorten your Product URLs :
Put the following at top of your /root/. htaccess file.
RewriteEngine On
#redirect /pages/product.php?product=name to /pages/product/name
RewriteCond %{THE_REQUEST} /pages/product\.php\?product=([^\s]+) [NC]
RewriteRule ^ /pages/product/%1? [L,R=301]
#Rewrite the new URL to the old one
#load the contents from the OLD URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^pages/product/([^/]+)/?$ /pages/product.php?product=$1 [L]

.htaccess on host gator is not removing .php extension and adding trailing slash

So I have recently uploaded my new website to hostgator.
I am trying to remove .php extension and add a trailing slash to all my urls.
For example
website.com/about.php should change to website.com/about/
I have the htaccess code that has worked for other websites on different hosts, but when i tried it for this website on hostgator it doesnt work.
This is the code I tried:
RewriteEngine On
RewriteBase /
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://website.co.uk/$1 [R,L]
RewriteCond %{HTTP_HOST} ^www.website.co.uk [NC]
RewriteRule ^(.*)$ http://website.co.uk/$1 [L,R=301]
Currently I now have this in my htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
# remove www and turn on https in same rule
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
Hostgator added this htaccess for me which basically redirects http requests to https and removes http://www.website.co.uk and www.website.co.uk to website.co.uk
What can I do to remove .php extension and force trailing slash?
You can replace all the code in your site root .htaccess with this:
RewriteEngine On
# remove www and turn on https in same rule
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(.*?)/?$ https://%1/$1/ [R=301,L,NE]
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=301,NE,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301,NE]
# To internally rewrite /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]
Make sure to test this in a new browser or after clearing your browser cache.
For starters, depending on your hosting environment you may need to add the following to the beginning of your htaccess file:
Options -MultiViews
If MultiViews is on the server will do implicit filename matching. So if you request page/ it could interpret that as page.php. Use the option above to disable MultiViews and see if that helps.

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.

how to remove question mark from two php URLs in .htaccess

I want to remove question mark from two URLs like this:
URL 1
http://localhost:8888/events/event.php?id=222
to
http://localhost:8888/events/event/222
URL 2
http://localhost:8888/membership/register.php?id=162
to
http://localhost:8888/membership/register/162
i'm use this code in .htaccess and just the 'event' page work:
Options -MultiViews
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /events/event(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /events/event/%1? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^events/event/([^.]+?)/?$ events/event.php?id=$1 [QSA,L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /membership/register(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /membership/register/%1? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^membership/register/([^.]+?)/?$ membership/register.php?id=$1 [QSA,L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]
You can use:
Options -MultiViews
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /events/event(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /events/event/%1? [R=302,L,NE]
RewriteCond %{THE_REQUEST} /membership/register(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /membership/register/%1? [R=302,L,NE]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# rewrite from pretty URL
RewriteRule ^events/event/([^.]+?)/?$ events/event.php?id=$1 [QSA,L,NC]
RewriteRule ^membership/register/([^.]+?)/?$ membership/register.php?id=$1 [QSA,L,NC]
# Rewrite with .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]

htaccess rewrite links and duplicate it

I have this htaccess code
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^einfogarden.com$
RewriteRule (.*) http://www.einfogarden.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/+single\.php\?title=([^\s&]+) [NC]
RewriteRule ^ %1/? [R=302,L,NE]
## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} \s/+(.*?)[^/][?\s]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=302]
# convert %20 to -
RewriteRule "^(\S*) +(\S* .*)$" $1-$2 [L,NE]
RewriteRule "^(\S*) (\S*)$" $1-$2 [L,R=302,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ single.php?title=$1 [NE,L,QSA]
which should remove single.php?title= from url and replace the 20% by slash and it work corrctly.
but i have 2 problem 1. it stop the css in my website 2. if you try to click any link (except) the home page it will give you something like this http://www.einfogarden.com/%D9%81%D9%88%D8%A7%D8%A6%D8%AF-%D8%A7%D9%84%D8%AC%D8%B1%D8%AC%D9%8A%D8%B1/single.php?title=%D9%81%D9%88%D8%A7%D8%A6%D8%AF%20%D8%A7%D9%84%D8%B1%D9%85%D8%A7%D9%86%20%D8%A7%D9%84%D8%B1%D8%A7%D8%A6%D8%B9%D8%A9
it duplicate the link
You are asking in the .htaccess the url of your current page.
So yourwebsite.com/home will be yourwebsite.com/home/home
I edited your code a little. using this will do fine.
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule ^(.*?)$ $1 [L]
RewriteCond %{REQUEST_URI} !^/cache
RewriteCond %{REQUEST_URI} !^/images
RewriteCond %{REQUEST_URI} !.*\.(css|jpg|gif|zip|js)
RewriteRule ^(.*)/(.*)/?$ index.php?page1=$1&page2=$2 [L]
RewriteRule ^(.*) index.php?page1=$1 [L]
#RewriteRule ^(.*)/?$ http://www.google.com/ [R]
This is a easy way the use SEO url's
The RewriteRule is for creating a new var to your url.

Categories