write .htaccess rewrite website URL multiple parameters - php

I´m new to .htaccess and I am having problems in rewriting some URLs.
My website is made with php and has 2 parameters: param1 and param2.
The URLs look like similar to:
www.website.com/index.php?param1=12345678
www.website.com/index.php?param1=09876543
www.website.com/index.php?param2=abcdefgh
www.website.com/index.php?param2=qwertzui
I would like to create a .htaccess file to remove “index.php”, replace param1 and param2 with 2 names and add “.html” at the end, so they became:
www.website.com/budget/12345678.html
www.website.com/budget/09876543.html
www.website.com/user/abcdefgh.html
www.website.com/user/qwertzui.html
I have got this code (copied from internet).
It removes the .php extension but in the internally forward it rewrite it at the end of the URL neglecting the parameters.
Is someone so kind to help me to write the code?
Thanks :)
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
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]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

You can try:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# handle ?param1=...
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?param1=([^\s&]+) [NC]
RewriteRule ^ /budget/%1.html? [R=301,L,NE]
# handle ?param2=...
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?param2=([^\s&]+) [NC]
RewriteRule ^ /user/%1.html? [R=301,L,NE]
## 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=301,NE,L]
RewriteRule ^budget/([^/.]+)/?$ index.php?param1=$1 [L,QSA,NC]
RewriteRule ^user/([^/.]+)/?$ index.php?param2=$1 [L,QSA,NC]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

Related

htaccess removal of backslash

I'm sure this question has come up before but I don't really know what to search.
my .htaccess file seems to be removing the '/' when I click links externally for the site.
For example, when I click "www.mysite.com/home" I'm directed to "www.mysite.comhome"
Here's my .htaccess
# Turn mod_rewrite on
Allow from all
RewriteEngine On
Options +FollowSymLinks -MultiViews
ErrorDocument 404 /errors/404.php
DirectoryIndex home.php
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(/{2,})(.*)$
RewriteRule . %1/%3 [R=301,L]
Thanks again !!
Problem is your last rule that is attempting to string multiple slashes from URLs.
You can use these rules in your site root .htaccess:
ErrorDocument 404 /errors/404.php
DirectoryIndex home.php
Options +FollowSymLinks -MultiViews
RewriteEngine On
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L,NE]
# strip multiple slashes
RewriteCond %{THE_REQUEST} //
RewriteRule ^.*$ /$0 [R=301,L,NE]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Make sure to use a new browser to avoid old cache or clean your browser cache completely before testing this change.

.htaccess rewrite rule not working?

I am using htaccess for generating clean urls in my project.
Like, I want to convert this url :
https://localhost/erp/profile.php?username=kopyo
to this url:
https://localhost/erp/profile/kopyo
My htaccess code is:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /erp
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# Code I am using is this line but not working
RewriteRule profile/(.*) profile.php?username=$1
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Let me know what I am doing wrong in this. Thanks
Make sure to use proper flags after RewriteRule and re-arrange your rules:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /erp/
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,NE,L]
RewriteRule ^profile/(.+)$ profile.php?username=$1 [L,NC,QSA]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

htaccess doesn't removing php extensions

I have a php project running. I am running my project on localhost. I want to remove php extensions from url in addressbar. I try several htaccess scripts but none worked some shows 404 not found error or doesn't work at all.
directory where my php project is D:\VertrigoServ\www\gotmation
I am using this htaccess script
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[\s?] [NC]
# RewriteRule ^ %1 [R=301,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/gta/$1.php -f
# RewriteRule ^(.+?)/?$ /gta/$1.php [L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I am newbie php programmer doesn't have much experience of this stuff. Any help will be appreciated.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)\.php
RewriteRule ^ /%1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This must work... you might have any other issues... do you have permitted .htaccess in apache config file???
try:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
These rules need to be in your document root.

mod_rewrite and .htaccess exception to the rule

I want to hide the .php extension throughout my site, I have this snippet and it works fine. However I have one URL that I want to be an exception to the rule, that is I want it have the .php intact and all it's $_GET params.
How can I include that exception into this snippet? And why does it work?
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
You can add exception like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule !^(exception1|exception2) %1/ [R=302,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
btw this code and comment look like something I would have written it in the past :)

Rewrite index when php is aready removed

I have a code in my htaccess that removes the .php from every page on my site. The following code is as far as i came, but what i actually want is that every page has an slash after the name (still without php)
and that de index(.php) name is completely removed.
Icouldn't find something on the internet having these two combined. I did find a code that created a slash at the end but that created many problems with internally redirecting.
Is all of this possible and if so, how?
Many thanks in advance,
Paul
Options +FollowSymLinks -MultiViews
RewriteEngine On
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Try:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ /%1/ [R,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)index\.php [NC]
RewriteRule ^ /%1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+?)/?$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule ^(.*?)/?$ /$1.php [L]

Categories