Remove parameters from url using .htaccess - apache

I want to remove parameters from url,
I need change this:
http://example.com/?page=about
To this:
http://example.com/about
How?
This is my .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]

Try the following in htaccess :
RewriteEngine On
#Remove php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)/?$ /$1.php [NC,L]
#rewrite /about to /?page=about
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /?page=$1 [NC,L]
this will rewrite
/about
to
/?page=about

Try this:
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?page=$1 [QSA,L]

Related

showing error on htaccess address rewrite

i am want to convert url:
/tld/?=com to /tld/com/
my full htaccess code is as below:
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+(whois|error)(?:\.php)?[\s?] [NC]
RewriteRule ^ /%1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(php?|jpg|gif|png|css|js|html|json|xml|eot|svg|ttf|woff|woff2|zip|csv|xlsx|webp|txt|gz|rar)$ [NC]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
##### Rewite #####
##RewriteRule ^([^/]*)\$ /tld.php?tld=$1 [L]
RewriteRule ^([^/]+)\$ /tld.php?tld=$1 [L]
but its show 500 error so please help
Have it this way:
Options -Indexes -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+(whois|error)(?:\.php)?[\s?] [NC]
RewriteRule ^ /%1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(php?|jpg|gif|png|css|js|html|json|xml|eot|svg|ttf|woff|woff2|zip|csv|xlsx|webp|txt|gz|rar)$ [NC]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
# ignore all rules below this for files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{ENV:REDIRECT_STATUS} .
RewriteRule ^ - [L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteRule ^tld/([\w-]+)/?$ tld.php?tld=$1 [L,QSA]

htaccess rewrite traling slash to GET parameter

I use this code to rewrite example.com/kat-something.html to example.com/kat.php?kat=something
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)-([^/]*)\.html$ $1.php?kat=$2 [QSA]
RewriteRule ^([^\.]+)$ $1.php [QSA]
But I want ?kat= to be / so example.com/kat/something.html to example.com/kat.php?kat=something so I tried:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)/(.*)\.html$ $1.php?kat=$2 [QSA]
RewriteRule ^([^\.]+)$ $1.php [QSA]
But it's not working. So how can I allow / to be an GET name?
You can try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^.]+)\.html$ $1.php?$1=$2 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^./]+)/?$ $1.php [L]

Rewrite htaccess so it doesn't need to repeat itself

I have one big htaccess file that does all the redirects for a site. Here is what I do for each directory:
RewriteRule ^CAKE_MIX/.php$ CAKE_MIX/index.php [R,L]
RewriteRule ^CAKE_MIX$ CAKE_MIX/ [R,L]
RewriteRule CAKE_MIX/faqs.php$ /faqs/faqs.php?cat=CAKE_MIX [QSA]
RewriteRule CAKE_MIX/index.php$ /overview/overview.php?cat=CAKE_MIX [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /CAKE_MIX/*
RewriteRule . /faqs/faqs.php?cat=CAKE_MIX&question=%{REQUEST_URI}
RewriteRule ^COOKIE_DOUGH/.php$ COOKIE_DOUGH/index.php [R,L]
RewriteRule ^COOKIE_DOUGH$ COOKIE_DOUGH/ [R,L]
RewriteRule COOKIE_DOUGH/faqs.php$ /faqs/faqs.php?cat=COOKIE_DOUGH [QSA]
RewriteRule COOKIE_DOUGH/index.php$ /overview/overview.php?cat=COOKIE_DOUGH [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /COOKIE_DOUGH/*
RewriteRule . /faqs/faqs.php?cat=COOKIE_DOUGH&question=%{REQUEST_URI}
And I do this for 18 different directories. How would I rewrite the htaccess to say something like "for any of the following directories: COOKIE_DOUGH, CAKE_MIX,ICE_CREAM, follow these rules?" and then the rules could be like:
RewriteRule ^DIRECTORY/.php$ DIRECTORY/index.php [R,L]
And would only have to be written once? Is that possible?
You can use regex alternation:
RewriteRule ^(CAKE_MIX|COOKIE_DOUGH|ICE_CREAM)/\.php$ $1/index.php [R,L]
RewriteRule ^(CAKE_MIX|COOKIE_DOUGH|ICE_CREAM)$ $1/ [R,L]
RewriteRule (CAKE_MIX|COOKIE_DOUGH|ICE_CREAM)/faqs\.php$ /faqs/faqs.php?cat=$1 [QSA]
RewriteRule (CAKE_MIX|COOKIE_DOUGH|ICE_CREAM)/index\.php$ /overview/overview.php?cat=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(CAKE_MIX|COOKIE_DOUGH|ICE_CREAM)/
RewriteRule . /faqs/faqs.php?cat=%1&question=%{REQUEST_URI}

htaccess rewrite 2 rule Simultaneously

I hava an htaccess file.it pass url to index.php
<!-- language-all: lang-html -->
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
now i wanna to remove "www" befor my domain name and also keep my previous RewriteRule .
How to do it؟
thanks
You can have a new rule for www removel before your current rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

Apache .htaccess to hide both .php and .html extentions

What lines should I add to remove both .html and .php extensions from my site?
I use this for just the .html part:
RewriteEngine on
RewriteBase /
RewriteCond %{http://jobler.ro/} !(\.[^./]+)$
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule (.*) /$1.html [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.html\ HTTP
RewriteRule ^([^.]+)\.html$ http://jobler.ro/$1 [R=301,L]
but I need for both file type extensions to be hidden from the same domain.
I know its late to answer but I giving it since it can be useful to someone :)
#Remove php extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
#Remove html extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
This will remove both php and html extension from your files and works perfectly.
You would want to test whether the file with .html or .php exists, and then redirect to such file if so.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_URI} \.((html)|(php))$
RewriteRule ^(.*)\.([^.]+)$ $1 [R,L]