HTACCESS Rewrite Rule for Query String in PHP - apache

I have my URL https://example.com/?hfgshdsds.
I need to rewrite a rule to makes that even If I removed the question mark , the link will works as same as before, so my url need to be https://example.com/hfgshdsds.
For the moment on my .htaccess file I have only the rule that open the php without extension .php .
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
Thank you!

With your shown samples, please try following htaccess Rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
##keep redirection Rules first then keep rest of rules.
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php/?$ %{REQUEST_FILENAME}.php [QSA,L]
##Adding new rule here for non-existing pages here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?$1 [QSA,L]

Related

htaccess - remove .php extension from url

I tried a lot of code to remove .php from url
for example - ht.abuena.net/presto.php -> ht.abuena.net/presto
and vice versa - internally
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
nothing works - the url stays unchanged
page is reloading by right clicking on the button Reload and choosing - Empty Cache and Hard Reload - so I hope the cache is cleared
live example here - here
With your shown samples, please try following htaccess rules file.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} ^$
##using THE_REQUEST variable for condition check.
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php/?\s [NC]
##Performing external redirect here.
RewriteRule ^ %1? [R=301,L]
##Performing rewrite for non-existing pages.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)/?$ /$1.php [QSA,L]
In my case if the project is core PHP one, I use the below lines in .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

dynamic url change issue

I want to change my URL from https://website.com/free?s=new-check to https://website.com/free/new-check/
My Current htaccess code :
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
RewriteCond %{THE_REQUEST} /free\?s=([^\s]+) [NC]
RewriteRule ^ /%1? [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ /free?s=$1 [NC,L]
It works and directed to https://website.com/new-check but what I want is https://website.com/free/new-check/
previously I removed the .php extension from the URL.
I tried a lot of solutions but nothing works. please help me
With your shown samples, please try following htaccess Rules. Make sure to keep these Rules at top of your htaccess file if you have other Rules also in your file.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
##https apply rules here.
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
##External redirect Rule here.
RewriteCond %{THE_REQUEST} \s/(free)\?s=([^\s]+)\s [NC]
RewriteRule ^ /%1/%2? [L,R=301]
##Internal rewrite rule here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*)/(.*)/?$ free.php?s=$1 [NC,QSA,L]

SEO Friendly URI using HTACCESS

I am new in PHP. I have my URL like below
http://localhost/index.php?page=20
http://localhost/user.php?id=15&localtion=delhi
http://localhost/folder/profile.php?id=15&active=today
What I am looking for convert my URL like below
http://localhost/page/20
http://localhost/user/id/15/localtion/delhi
http://localhost/folder/profile/id/15/active/today
I am trying from many hours to make it working with .htaccess file but no luck yet
one of little working code is like below
RewriteEngine on
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
its removing .php from URL like below
http://localhost/index?page=15
I have checked many questions and answer in stackoverflow like below
https://stackoverflow.com/questions/41390397/htaccess-seo-friendly-url
https://stackoverflow.com/questions/41439512/php-url-replace-question-mark-and-parameter-with-slash
https://stackoverflow.com/questions/12451886/htaccess-for-friendly-url-with-multiple-variables
but not able to achieve my goal. Let me know if anyone expert here can help me for doing same and still my php code work fine without any issue.
Thanks a lot!
With your shown samples, please try following htaccess rules. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rule for external rewrite.
##Rule to handle from http://localhost/index.php?page=20 to http://localhost/page/20
RewriteCond %{THE_REQUEST} \s/index\.php\?([\w-]+)=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Rule for external rewrite.
##Rule to handle from http://localhost/user.php?id=15&localtion=delhi TO http://localhost/user/id/15/localtion/delhi
RewriteCond %{THE_REQUEST} \s/(user)\.php\?([\w-]+)=(\d+)&([\w-]+)=([\w-]+)\s [NC]
RewriteRule ^ /%1/%2/%3/%4/%5? [R=301,L]
##Rule for external rewrite.
##Rule to handle from http://localhost/folder/profile.php?id=15&active=today To http://localhost/folder/profile/id/15/active/today
RewriteCond %{THE_REQUEST} \s/(folder)/(profile)\.php\?([\w-]+)=(\d+)&([\w-]+)=([\w-]+)\s [NC]
RewriteRule ^ /%1/%2/%3/%4/%5/%6? [R=301,L]
##internal rewrite rule to handle files internally.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ $1/$2.php?$3=$4&$6=$6 [QSA,L]
##internal rewrite rule to handle files internally.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ $1.php?$2=$3&$4=$5 [QSA,L]
##internal rewrite rule to handle files internally.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ index.php?$1=$2 [QSA,L]

.htaccess remove .html and force redirect to extensionless url

I'm trying to remove .html extensions from URLs and to have 302 redirection to the extensionless URLs like this:
http://example.com/file.html -> http://example.com/file
I have looked at so many stackoverflow answers, tried them but unfortunately no success. I came out with this and I don't understand why it does not work:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteCond %{REQUEST_URI} \.html$
RewriteRule (.*)\.html$ /$1 [R=302,L]
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteCond %{REQUEST_URI} !\.html$ [NC]
RewriteRule ^(.*)$ $1.html
Where the first block should redirect to the extensionless version and the second block should find the file.
You need to use %{THE_REQUEST} here to prevent rewrite loop error. otherwise without THE_REQUEST variable Rule keeps rewriting file => file.html=>file=>file.html..
RewriteEngine On
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule (.*) /%1 [R=302,L]
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

htaccess remove .php extension from the following

I have this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ ./index.php?path=$1 [L,NC,QSA]
I need the .php extension removed from the browser address
This is meant to answer your comment below the question:
RewriteEngine on
RewriteRule ^(.*)\.php(.*)$ $1$2 [QSA,L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ ./index.php?path=$1 [L,NC,QSA]
However I do not really see a point in this. Where should the ".php" extension in those urls come from? I mean users won't write down a url to query your system themselves. Instead they click on a link and that link must be something you sent out before. So obviously your approach should be to send out links that do not contain that file name extension in the first place!
You can use these 2 rules for hiding .php extension:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)$ $1.php [NC,L]