I am trying to beautify my URLs and want to get rid of the .php file extension. I've found a couple of other questions here such as this one (Removing the PHP file type extension) But none of the suggestions work for me. They send me to my 404.php or do not change the URL at all.
I figure the entire RewriteEngine code in my htaccess as a whole is conflicting in itself.
Here's what I got so far,
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /404.php [R]
</IfModule>
I would like my domains to look like
https://www.example.com/about
Don't mix Redirect, RedirectMatch and RewriteRule directives in same .htaccess as they come from different Apache modules and their load sequence is unpredictable.
Have it this way:
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Make sure to clear your browser cache before testing this change.
Put in .htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
#1)externally redirect "/file.php" to "/file"
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
#2)Internally map "/file" back to "/file.php"
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,L]
ErrorDocument 404 ./404
RewriteCond %{REQUEST_URI} ^404/$
RewriteRule ^(.*)$ ./404 [L]
Redirect 301 /index /
remove .php from the link :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Related
I have a htaccess file for a site to let it's script work . I don't know much about how it works, but I have to remove the trailing slash at the end.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
ErrorDocument 404 https://www.onlinegames.nl/
## 301 Redirects
# 301 Redirect 1
RewriteCond %{HTTP_HOST} ^www\.onlinegames\.nl$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTPS} =on
RewriteRule ^index\.html$ https://www.onlinegames.nl/? [R=301,NE,NC,L]
<IfModule mod_headers.c>
<FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
</IfModule>
How can I remove the trailing slash at the end? I've tried to change a few things, but most of the times the script is not working correctly anymore and all the subpages are not working anymore.
You may use these rules in your site root .htaccess to replace your shown code. Note that ordering of these rules is also important and change in ErrorDocument.
ErrorDocument 404 /
RewriteEngine On
# remove www from host names
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# redirect http to https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/+$
RewriteRule ^ %1 [R=301,NE,L]
# strip /index.html
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.html$ / [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [L]
so I got this script from codester and I wasn't able to install it cause I keep getting errors like ".htaccess misconfigured" can anyone detect the error for me please, here is the .htaccess code
RewriteEngine on
RewwriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
RewriteRule page/(.*)$ page.php?slug=$1 [L]
RewriteRule py$ _py.php [L]
RewriteRule payout$ pyt.php [L]
RewriteRule contact$ ct.php [L]
Options -Indexes
ErrorDocument 404 /404.php
Help
There is a spelling mistake in your second line, use
RewriteBase /
instead of RewwriteBase /
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
RewriteRule page/(.*)$ page.php?slug=$1 [L]
RewriteRule py$ _py.php [L]
RewriteRule payout$ pyt.php [L]
RewriteRule contact$ ct.php [L]
Options -Indexes
ErrorDocument 404 /404.php
I am getting 500 Internal Server Error when i wrote the twice file name from the url.
I am using the following RewriteRule for the user Profile url:
RewriteRule ^profile/([^/]+)/?$ profile.php?username=$1 [L,QSA,NC]
RewriteRule ^profile/(followers|friends|saved|stories)/([^/]+)/?$ $1.php?username=$2 [L,QSA,NC]
The Rules working working fine with this link:
https://www.thewebsiteurl.com/profile/username
But if i wrote the file name twice like this
https://www.thewebsiteurl.com/profile/profile/username
then i am getting the 500 Internal Server Error.
Is there any answer here to fix this problem ?
Here is my Full of my HTACCESS codes:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
ErrorDocument 404 https://www.websiteurl.com/sources/not-found.php
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index\.php$ /$1 [L,R=302,NC,NE]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^profile/([^/]+)/?$ profile.php?username=$1 [L,QSA,NC]
RewriteRule ^profile/(followers|friends|saved|stories)/([^/]+)/?$ $1.php?username=$2 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin '*'
</IfModule>
Rules appear to fine though I have seen in some web servers that %{REQUEST_FILENAME}.php doesn't work properly due to file name resolution issues.
Can you try changing that rule to this:
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
The following redirect is preventing a form with php_self from running functions on the same page properly.
I was having trouble removing the trailing slash for some reason on this server so I went this route. Found it interesting! The page refreshes but runs no code. Changing the page to prevent redirect goes back to working. Any thoughts?
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
DirectorySlash Off
RewriteRule ^bio$ /bio/index.php [L,E=LOOP:1]
RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^bio/$ /bio [R=301,L]
RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^bio/index.php$ /bio [R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
You need to keep DirectorySlash off if you don't want / at the end of a directory:
DirectorySlash off
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule ^(.+?)\.php$ /$1 [L,R=301]
RewriteRule ^(.+?)/index$ /$1 [L,R=301]
# remove trailing slash
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=302,L]
# route to bio/index.php if request is /bio
RewriteRule ^bio$ bio/index.php [L,NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
I plan on implementing this .htaccess code (thanks anubhava) into my website:
RewriteEngine on
rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://mydomain.com/$1 [r=301,nc]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
RewriteRule ^(.+?)/?$ $1.html [L]
ErrorDocument 404 /404.html
Basically it will change URL from "www.mydomain.com/this-page.html" into "www.mydomain.com/this-page" - it does that very well.
However, I have encountered an issue - let's say that one of my pages is "treatments.html".
URL adress will look like this "mydomain.com/treatments" and that's fine. But I want also to make a directory called "treatments" and include the list of available treatments in that folder, it would look like this
"mydomain.com/treatments/treatment-one"
"mydomain.com/treatments/treatment-two"
"mydomain.com/treatments/treatment-three"
and so on...
THE ISSUE IS:
When I try to access the file "treatments.html" browser confuses it with the directory and server sends message [403]Forbidden.
Any ideas for solution?
Ok change your last rule to:
DirectorySlash On
ErrorDocument 404 /404.html
RewriteEngine on
RewriteBase /
rewritecond %{http_host} !^mydomain\.com$ [nc]
rewriterule ^(.*)$ http://mydomain.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
RewriteRule ^(.+?)/?$ $1.html [L]