Remove .php extension and add one slash - .htaccess

good day
i am just stuck in the middle of something i want to remove .php extension and make one slash for example
my current url is
http://example.com/campus.php?college=BGIET,%20Sangrur
Now i want to change it to
http://example.com/campus/BGIET,Sangrur
Is it possible to do this i am clueless to how to make this happen
and yes /mod_rewrite is enabled
proof :-
RewriteEngine On
RewriteRule ^google.html$ http://www.google.com/ [R=301]
Going to http://www.example.com/google.html redirecting to Google.com so it is enabled..
help me out please ( ._.)

You can use this code in your DOCUMENT_ROOT/.htaccess file:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /campus\.php\?college=([^\s&]+)\%20([^\s&]+) [NC]
RewriteRule ^ campus/%1%2? [R=302,L,NE]
RewriteCond %{THE_REQUEST} /campus\.php\?college=([^\s&]+) [NC]
RewriteRule ^ campus/%1%2? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^campus/(.+?)/?$ campus.php?college=$1 [NC,L,QSA]

Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteCond %{THE_REQUEST} \ /campus\.php\?college=([^\s&]+) [NC]
RewriteRule ^ /campus/%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^campus/(.*)$ /campus.php?college=$1 [NC,L]
The first rule makes sure that if the old URL is used, the user gets redirected to the new one automatically. The second rule resolves the non-php link to the php one behind the scenes.

Related

Url rewriting rule with .htaccess file

I'm here to ask for advice about the .htaccess file that I can't configure as I would like.
I have a site, let's say :
www.mysite.com/php/blog.php
I would like the visitor to see in the address bar only:
www.mysite.com/blog
This is what the .htaccess file contains:
Options +FollowSymlinks
Options All -Indexes
RewriteEngine On
RewriteRule ^$ index.php #This line is to hide index.php on the home page, maybe it's not the right way to do it
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^php/([0-9a-zA-Z]+).php$ $1 [L] #I tried to write this, but it doesn't work
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I suspect that this question has been asked many times, but I have really tried, without success.
Can you help me, please?
Thanks !
Your patter is wrongly expecting URI to start with /php/ which is not the case here. Also keep http -> https rules at top and then handle .php extension rule like this:
RewriteEngine On
# http -> https redirect
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# To externally redirect /php/file.php to /file
RewriteCond %{THE_REQUEST} \s/+(?:php/)?(\S+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# To internally forward file to /php/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/php/$1.php -f
RewriteRule ^(.+?)/?$ php/$1.php [L]

Clean URLs in a subfolder

I've read and followed guides and looked for answers to other people's questions, but I'm struggling with url rewriting and htaccess.
I have a subfolder on my site with an index page which handles query strings to bring up dynamic content. I'd like the variable to appear to be a subfolder name e.g.
http://mywebsite.com/subfolder/index.php?v=variable
to
http://mywebsite.com/subfolder/variable
The .htaccess file I've made is at http://mywebsite.com/subfolder/ i.e. the same folder as index.php
How can I get this to work? Any help gratefully appreciated.
You can use these rules inside your /subfolder/.htaccess
RewriteEngine On
RewriteBase /subfolder/
RewriteCond %{THE_REQUEST} \s/subfolder/index\.php\?v=([^&\s]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ index.php?v=$1 [L]
Update (passing two values)
RewriteEngine On
RewriteBase /subfolder/
RewriteCond %{THE_REQUEST} \s/subfolder/index\.php\?v=([^&\s]+)&v2=([^&\s]+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteCond %{THE_REQUEST} \s/subfolder/index\.php\?v=([^&\s]+)\s [NC]
RewriteRule ^ %1? [R=301,L]
# Don't touch to existing files/folders
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Rewrite /xxx to /index.php?v=xxx
RewriteRule ^([^/]+)$ index.php?v=$1 [L]
# Rewrite /xxx/yyy to /index.php?v=xxx&v2=yyy
RewriteRule ^([^/]+)/([^/]+)$ index.php?v=$1&v2=$2 [L]
Use this in your .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} v=(.+)$ [NC]
RewriteRule ^ subfolder/%1? [R=301,L,NE]
This grabs the variable using %{QUERY_STRING} and then appends it to the rewrite using %1. You'll see I've added a ? onto the end of the rewrite. That is to stop the original query from appearing on the end of the URL.
I've used R=301 which is a permanent redirect. You might want to changes this to R=302 while you're testing, as this is temporary.
You can view a test of this rule working here: https://htaccess.madewithlove.be?share=7b6832e9-2c05-5d1d-916c-e4dd0f5b1da6
Make sure you clear your cache before testing this.

htaccess - HTTP to HTTPS and .HTML to /

I need help with an .htaccess that:
forces http:// to https://
AND
forces .html to /
What I have so far:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Options -MultiViews
RewriteEngine On
#1 This line checks if the https is off
RewriteCond %{HTTPS} ^off$
#then, redirect to https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [NC,L,R]
#2 this line checks if the request is /file.html
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
#then redirect /file.html to /file
RewriteRule ^ /%1 [NC,L,R]
#3 if the request is not for dir
RewriteCond %{REQUEST_FILENAME} !-d
#and the request is an existing filename
RewriteCond %{REQUEST_FILENAME}.html -f
#then rewrite /file to /file.html
RewriteRule ^([^/]+)/?$ $1.html [NC,L]
In the example above, the first condition is met when the original scheme is HTTP, and then the rule is processed. HTTP goes to HTTPS. The first Round of rewrite processing ends here.
In the second round, mod_rewrite accepts the URI /file.html and the rule redirects it to /file, since the /file does not exist in directory, so we need to rewrite it to the original file #3...
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
I've used both of them before, but I haven't used them together. Hope this helps.
(EDIT)
To actually FORCE extensions to look like directories:
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=302,NE,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
RewriteRule ^(.+?)/?$ $1.html [L]
http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/
http://www.inmotionhosting.com/support/website/ssl/how-to-force-https-using-the-htaccess-file
First of all, make sure mod-rewrite is enabled.
Then, put this code in your htaccess (which should be in document root folder)
Options -MultiViews
RewriteEngine On
RewriteBase /
# Redirect http urls to https equivalent
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://{HTTP_HOST}/$1 [R=301,L]
# Redirect existing /path/file.html to /path/file/
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} \s/(.+?)\.html\s [NC]
RewriteRule ^ %1/ [R=301,L]
# Internally rewrite back /path/file/ to /path/file.html (if existing)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f
RewriteRule ^(.+)/$ $1.html [L]
Warning:
Make sure to serve the same document root as http for https (apache ssl block configuration)
Be aware that this creates virtual directories (by adding a trailing
slash) which could mess up your html resources (if you're using
relative paths instead of absolute paths). If so, use absolute paths
instead

.htaccess RewriteRule not overriding URL

I have this .htaccess which basically is supposed to turn my links from
http://www.dsbigiena.ro/products.php?product-id=Colac-wc-cu-folie-de-plastic-cu-actionare-manuala-prin-buton
to
http://www.dsbigiena.ro/Colac-wc-cu-folie-de-plastic-cu-actionare-manuala-prin-buton/
Unfortunately, it's not doing it.
Did I write it wrong?
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]*)/$ /products.php?product-id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
EDIT: I did indeed test the .htaccess with "deny from all", and it works.
Your current rewrite rule only resolve a pretty URL. It won't convert an actual /products.php URL to the pretty one by itself. You need to have an explicit rule for that as
RewriteEngine On
RewriteBase /
# Redirects from a normal URL to a pretty one
RewriteCond %{THE_REQUEST} \ /products\.php\?product-id=([^\ ]+) [NC]
RewriteRule ^ /%1/? [L,R=301]
# Resolves the pretty URL
RewriteRule ^([^/]+)/?$ /products.php?product-id=$1 [QSA,L]

remove php exentsion not working in .htacces

I have a website, and the builder put in this code into my .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]*)(\.php)?(\?*)$ index.php/$1$3 [L,QSA]
But, what i also need is:
- remove slash if it is no directory
- remove extension .php
- remove index.php if it is in de url
Ive tryed many suggestions, but i think it is in the code that is already in de .htaccess.
i've managed to redirect the site to www. if not given in the url
Does anyone have an idea? i'm out of ideas
Before the rules that you already have, try adding these:
# remove trailing slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# remove index.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /(.*)index\.php/?([^\?\ ]*)
RewriteRule ^ /%1%2 [L,R=301]
# remove php extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /(.*)\.php($|\ |\?)
RewriteRule ^ /%1 [L,R=301]

Resources