Simple rewriting rule - regex

Ok, maybe I'm dump, but my simple rewrite .htacces rule doesn't work. I just want on my site for example www.mysite.com/something.html rewrite rule to www.mysite.com/something . I yust want to remove .html and getting content from file something.html
Now I have this in my .htacces file, but not working:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
AddType application/x-httpd-php5 .htm .html

You need another rule for redirecting .html:
RewriteEngine On
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
AddType application/x-httpd-php5 .htm .html

Related

404 when redirecting with .htaccess because it's deleting sub-directory

What I want to do is a URL friendly site by deleting the .html. This is my .htaccess:
RewriteCond %{THE_REQUEST} \s/+rscares/(.*?)\.html[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]
#example.com/page will display the contents of example.com/page.html
RewriteEngine On
RewriteBase /rscares/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
This is the thing, I'm testing in a sub-folder so if I type example.com/folder/page works fine but if I do example.com/folder/page.html it redirects to example/page and I get a 404 because the page doesn't exist. I want to redirect to the same label it was before the redirection. Any help? Thanks.
You can use this code:
RewriteEngine On
RewriteBase /rscares/
RewriteCond %{THE_REQUEST} \s/+(rscares/.+?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

htaccess remove .php, .html and trailing slash

i am trying to remove .php .html and traling slash from url using htaccess,how ever this code working fine for php but not for html and trailing slash
For PHP
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
For HTML
RewriteCond %{REQUEST_URI} index.html
RewriteRule ^(.*)index.html$ /$1/ [R=301,L]
Have it this way:
# externally redirect /dir/file.php to /dir/file and remove index
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(?:html?|php)/?[\s?] [NC]
RewriteRule ^ /%1%2 [R=301,L,NE]
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
# internally add php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# internally add html extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

How to remove .php or .html extension from single page?

I have a website
eg : www.abcd.com in that there are many pages.
eg : www.abcd.com/one.php , www.abcd.com/two.php
I just want to remove the .php from www.abcd.com/one.php and not from all the other pages.
I have tried this part in .htaccess file.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
You can use this rule in your root .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(one)/?$ /$1.php [L,NC]

Redirecting /page.html to /page doesn't work inside subdirectory

I'm trying to redirect from /page.html to /page inside of a subdirectory, but it doesn't work.
What I do: http://example.com/subdirectory/page.html
What I get: http://example.com/page
What I want: http://example.com/subdirectory/page
.htacces file:
RewriteEngine On
#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
How can I get this to work?
Use this code for .html hiding in your DOCUMENT_ROOT/.htaccess file:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1 [L,NC,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ /$1.html [L]
Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
</IfModule>
Thanks

hide .php extension & force trailing slash

I've been trying to delete the .php extension and force trailing slashes in the end of the URLs.
I've found an answer to do it exactly what I need at (.htaccess trouble with hiding file extension and forcing trailing slash) but it does not work, I believe it's due to how I am forcing my website to change the document root.
As I do not have access to Virtual Hosts I must change my document root using htaccess, and I have accomplished it by applying the following code..
RewriteCond %{HTTP_HOST} ^site.co$ [NC]
RewriteRule ^(.*)$ http://www.site.co/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !webroot/
RewriteRule (.*) /webroot/$1 [L]
The only working solution that I've been able to apply to hide the .php extension is with the code below.
removing .php extension(only working solution)
RewriteCond %{THE_REQUEST} ^\w+\ /(.*)\.php(\?.*)?\ HTTP/
RewriteRule ^ http://%{HTTP_HOST}/%1 [R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .* $0.php
This code is in the thread that I have linked above, but it does not work on my website as I believe it's due to the document root.
RewriteEngine on
# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?[^/])$ /$1/ [R=301,L]
# .php ext hiding
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
Try these modified rules with different ordering:
RewriteEngine on
# add www before hostname
RewriteCond %{HTTP_HOST} ^site\.co$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]
# if on article page, get slugs and make into friendly url
RewriteCond %{THE_REQUEST} \s/article\.php\?article_uid=([^&]+)&article_title=([^&\ ]+)
RewriteRule ^ /article/%1/%2/? [L,R=302,NE]
# if page with .php is requested then remove the extension
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L,NE]
# Force a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s/+([^.]+?[^/.])[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L]
# allow page direction to change the slugs into friendly seo URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?:^|/)article/([^/]+)/([^/]+)/?$ /webroot/article.php?article_uid=$1&article_title=$2 [L,QSA,NC]
# silently rewrite to webroot
RewriteCond %{REQUEST_URI} !/webroot/ [NC]
RewriteRule ^ /webroot%{REQUEST_URI} [L]
# .php ext hiding
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]