How to remove parameter from URL - apache

I apologise if this question is asked before but I can't find the specific instance for my problem.
How to achieve this www.example.com/index.php?user=john to www.example.com/john
I am using this already to remove the .php extension but it doesn't fix the user parameter.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R]
ErrorDocument 404 /error.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/([A-Za-z0-9_]+) ?user=$1
I highly value any reply. Please help.

With your shown attempts, please try following htaccess rules. Considering that you have to pass your arguments to index.php file in backend.
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect rules here.
RewriteCond %{THE_REQUEST} \s/index\.php\?user=(\S+)\s [NC]
RewriteRule ^ /%1? [R=301,L]
##Internal rewrite rules here..
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ index.php?user=$1 [QSA,L]

Related

RewriteRule - remove params keys and keep values in address bar

RewriteEngine ON
RewriteRule ^video$ video.php [L]
the above line works
example.com/video is interpret as example.com/video.php
now I need example.com/video?id=5&s=lorem-ipsum
to interpret as example.com/video/5/lorem-ipsum - and vice versa
RewriteCond %{THE_REQUEST} video?id=([^\s&]+)&s=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteRule ^([\w-]+)/([\w-]+)/?$ video?id=$1&s=$2 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/?$ video?/$1/$2 [L,QSA]
doesn't work - in both directions
pls help
With your shown attempts, please try following .htaccess rules file. These rules are assuming you want to infernally rewrite to index.php you can change php file name in 2nd set of rules as per your requirement.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect Rules from here..
##Redirect 301 to example.com/video/5/lorem-ipsum from example.com/video?id=5&s=lorem-ipsum
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s/(video)\?id=([^&]*)&s=(\S+)\s [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]
##Internal rewrite for example.com/video?id=5&s=lorem-ipsum
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^video/([^/]*)/(.*)/?$ index.php?id=$1&s=$2 [QSA,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]

How do I edit apache .htaccess correctly?

Good day! There are 3 tasks that I want to implement using the .htaccess file:
Work only with https protocol
Create a 301 redirect from https://www.example.com to https://example.com
Redirect all requests to the index.php file
My best version of .htaccess looks like this, but I'm not sure if this is the right decision:
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{HTTP_HOST} .* [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php
I would be grateful for any advice!
Could you please try following, written as per your shown samples. Please make sure you clear your browser cache before you test your URLs.
RewriteEngine ON
##For applying https on each request.
RewriteCond https !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
##For removing www from request.
RewriteCond %{HTTP_HOST} ^(?:www\.)(.*)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,NE,L]
##For applying index.php for non existing files or directories.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

How rewrite url in htaccess

Good Morning,
I have documents within the structure of my website, which are accessed with the following url format:
http://example.com/docs/files/123/mydoc.pdf
http://example.com/docs/files/475/otherdoc.pdf
I want when a url of the above type is accessed, it is renamed with the following format:
http://example.com/123/mydoc
http://example.com/475/otherdoc
I have the following rules in the htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteRule ^/docs/files/([0-9]+)/([a-zA-Z0-9-]+)\.([a-zA-Z-]+) /$1/$2 [QSA,L]
</IfModule>
If I type in the browser
http://example.com/docs/files/123/mydoc.pdf
It doesn't change the url to
http://example.com/123/mydoc
What am I doing wrong?
Thanks
You may try these rules in your site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+files/(\S+)\.pdf[?\s] [NC]
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/files/$1.pdf -f
RewriteRule ^(.+?)/?$ files/$1.pdf [L]
With your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s(/files/.*)\.pdf\s [NC]
RewriteRule ^ http://%{HTTP_HOST}%1 [R=301,NE]
RewriteRule ^(.*)/?$ files/$1.pdf [L]

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]