301 redirection instead of 302 redirection - php

I have used this code so far in my local (it's working fine).
RewriteEngine On
Options -Indexes
RewriteBase /projectname/
## 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]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
redirect 301 /projectname/index/ http://localhost/projectname/
redirect 301 /projectname/about/index/ http://localhost/projectname/about/
redirect 301 /projectname/agent/index/ http://localhost/projectname/agent/
redirect 301 /projectname/privacy/index/ http://localhost/projectname/privacy/
RewriteCond %{THE_REQUEST} //
RewriteRule .* $0 [R]
I place this in root .htaccess file. But i have three subfolders like about, agent, privacy also.
I used this code for
remove .php extension (except index pages in all folders include root).
add trailing slash
remove more than one trailing slashes
remove index.php in all folders (including root and subfolders).
(Note: Same thing applied for all pages inside subfolder as well)
But these pages redirected from 302 and then redirect 301.
I need to redirect only 301 include all the options specified above (points).
Any one please amend these redirection rules.
Any one please help me ?

Your htaccess could be simplified this way in order to respect your criteria
DirectoryIndex index.php
Options -MultiViews
RewriteEngine On
RewriteBase /projectname/
# redirect "*/index.php" to "*/"
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} \s/(.+/)?index\.php\s [NC]
RewriteRule ^ %1 [R=301,L]
# redirect "*/page.php" to "*/page/"
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} \s/(.+)\.php\s [NC]
RewriteRule ^ %1/ [R=301,L]
# rewrite back "*/page/" to "*/page.php"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.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.

Access PHP files without typing extension but also allow folders default to index.php

I have the following .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
To my limited understanding of Apache htaccess rules this is doing two things:
Redirecting http:// to https://
Allow access of domain.com/file.php files as domain.com/file
The problem is that rule #2 thinks every call to anything /name is a name.php when sometimes is an actual folder, and I need it to redirect to that folder's index.php file by default.
Any ideas?
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=307,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_FILENAME}.php [L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_FILENAME}.html [L]
## Redirect to HTTPS. Remove if you do not want this.
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
## Disables indexing (Always a good idea :D)
Options -Indexes
Remove the last line and try replacing with:
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
You need to check if %{REQUEST_FILENAME } exists as an existing php file before rewriting it:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https: //%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

htaccess unable to remove .php extension on Godaddy Linux

I know am re requesting the question which are already asked before but i tried all the posts and did all the research but still am unable to over come the problem i tried to put the below second snip first to htaccess in www directory and as well as folder directory the link of the page later i added the small snip it to the htaccess also but still its not working very disappointed my hosting provider is Godaddy if i add .php it works
htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
second
Options -MultiViews
RewriteEngine On
RewriteBase /
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{HTTP_HOST} ^www\.elmorfeo\.com
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)index$ $1 [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
Following one works for me
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,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
I found the answer that was all the above written ware correct but godaddy Cpanel there is no option to show hidden files i accessed it via ftp i found the original the correct htaccess file i did the changes there problem solved

6 .htaccess Rewrites: Remove index.html, Remove .html, Force non-www, Force Trailing Slash

i've to give some information about my website Environment
i have static webpage in the root.
Wordpress installed in sub-dictionary www.domain.com/blog/
I have two .htaccess , one in the root and one in the wordpress
folder.
i want to
www to non on all URLs < below code DID it :)
Remove index.html from url < below code DID it :)
Remove all .html extension / Re-direct 301 to url
without .html extension < below code DID it :)
Add trailing slash to the static webpages / Re-direct 301 from non-trailing slash << I NEED HELP WITH THAT
Force trailing slash to the Wordpress Webpages / Re-direct 301 from non-trailing slash < below code DID it :)
Some examples
domain.tld/index.html >> domain.tld/
domain.tld/file.html >> domain.tld/file/
domain.tld/file.html/ >> domain.tld/file/
domain.tld/wordpress/post-name >> domain.tld/wordpress/post-name/
My code in ROOT htaccess is
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
#removing trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]
#www to non
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?domain\.com)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]
#html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]
#index redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://domain.com/ [R=301,L]
RewriteCond %{THE_REQUEST} \.html
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /blog/
# Force Trailing Slash for wordpress
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)[^/]{1}$ %{REQUEST_URI}/ [L,R=301]
</IfModule>
The above code do
redirect www to non-www
Remove trailing slash at the end (if exists)
Remove index.html
Remove all .html
Redirect 301 to filename without trailing slash at the end
Force Trailing Slash for wordpress and redirect 301 from non trailing slash at the end
EDIT
#removing trailing slash Rule usage
Have it this way for site root .htaccess:
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
#www to non
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?domain\.com)$ [NC]
RewriteRule ^(.+?)/?$ http://%1/$1/ [R=301,L]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html/?[\s?] [NC]
RewriteRule ^ /%1/ [R=301,NE,L]
#index redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://%{HTTP_HOST}/ [R=301,L]
# add a trailing slash to non files
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=301,NE]
# add html internally
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)/$ $1.html [L]
</IfModule>
Make sure to clear your browser cache before testing.
RewriteEngine On
RewriteBase /
WWW to Non
RewriteCond %{HTTP_HOST} ^www.domain\.tld$ [NC]
RewriteRule ^(.*)$ http://domain.tld/$1 [L,R=301]
Remove index.html from url
RewriteRule ^index.html$ / [L,R=301]
Remove all .html extension / Re-direct 301 to url without .html extension
RewriteRule ^([a-zA-z0-9]+).html$ /$1 [L,R=301]
Add trailing slash to the static webpages / Re-direct 301 from non-trailing slash
RewriteRule ^([a-zA-z0-9]+).html$ /$1/ [L,R=301]
Force trailing slash to the Wordpress Webpages / Re-direct 301 from non-trailing slash
RewriteRule /blog/([^/]+) /blog/$1/ [L,R=301]

mod_rewrite rule 500 internal server error

I want to redirect from extension URL to extensionless one. For example, /contact should read the contents of /contact.php. I use the following rules to achieve this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
There is a file called test.php that has a variable called "id". I want to redirect read requests from /test/1 to test.php?id=1
I use the following to achieve this:
RewriteRule ^test/([0-9]+)/?$ /test?id=$1
For some reason calling the test/1 file leads to a 500 internal server error. If I remove the rules to hide the php extension, it works again
Also how can I force a permanent redirect on the URL with extension? For example if someone ties to reach /contact.php should be redirected to /contact
Thanks
You can use this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# your existing rule (fixed)
RewriteRule ^test/([0-9]+)/?$ /test.php?id=$1 [L,NC,QSA]
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
## To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L]
This is in my htaccess ... Be sure to have mod_rewrite turned on in Apache...
RewriteEngine On
# Redirect .php requests to url without an extension
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]
RewriteRule ^([^/.]+)$ $1.php [L]
# This will remove the trailing slash unless it's a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]

Categories