1

I have a website where there are many directories like test so when I open my website with domain.com/test it gives me domain.com/test/ I want this last slash to be removed.

I also want to remove the extention so that when I open a page like page.php it gives me domain.com/page

In addition, I want to send all the error pages to domain.com/error404 and I do have added code for non http to https

This is my current attempt:

RewriteEngine On 
RewriteBase / 
Options All -Indexes

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L]

ErrorDocument 403 /www.domain.com/error404.php
ErrorDocument 404 /https://www.domain.com/error404.php

https is working correctly but I also want to remove the last slash and extensions and handle error pages which currently do not work.

Henders
  • 1,195
  • 1
  • 21
  • 27
LIsa
  • 65
  • 7

2 Answers2

0

You can turn of directory slashes using:

DirectorySlash Off

or you can remove that trailling slash with:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [R=302,NE,L]

and redirecting the errors can be:

ErrorDocument 404 /path/to/error404
Blueblazer172
  • 588
  • 2
  • 15
  • 44
0

You can use these rules in your site root .htaccess:

ErrorDocument 403 /error404.php
ErrorDocument 404 /error404.php

RewriteEngine On

# redirect to https
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301,L,NE]

# remove index.php
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]

# To externally redirect /dir/file.php to /dir/file
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]

# To internally forward /dir/file to /dir/file.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Sir, i dont want to show any index in url. right now i am its like domain.com/index/ and also the images do not load becasue there is no index folder in the site. can wo hide the index from url make it like domain.com or domain.com/folder-name – LIsa Dec 09 '16 at 11:31
  • i think there is problem with post data while submiting the form .. PLease update this sir, the form post data has some issues .. – LIsa Dec 10 '16 at 22:05
  • can you help me here please https://stackoverflow.com/questions/58888268/php-codeigniter-htaccess-remove-controller-name-and-index-php-and-rewrite-url – LIsa Nov 16 '19 at 09:20