Currently, my site is set up to redirect a .php to a non-php version. Problem with this is when I checked, it is issuing a "200 OK" for both pages, which I know is a no no with google.
I want to 301 redirect the .php page to the non-php. It is the same page, and I have already added the canonical link to the page, reflecting the non-.php page.
How do I set up the 301? I am currently handling the .php to direct to non-.php page using .htaccess with the following code:
# Remove filename extension
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [QSA,NC,L]
</IfModule>
Any ideas how to resolve the 200 OK to 301 redirect the .php page (note they are the same page)?
You need one additional rule:
# Remove filename extension
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# To externally redirect /dir/abc.php to /dir/abc
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [QSA,NC,L]
</IfModule>
Place this rule before your existing rule.
This will externally redirect all .php file to non .php version. Thus Google will not index your .php file.
Related
I have a 404 error page and tried the below code which was generated from this link. But when the wrong URL is entered it redirects to the main page, not to the 404 error page
<IfModule mod_rewrite.c>
RewriteEngine On
ErrorDocument 404 https://www.rcis.in/404.php
</IfModule>
I have added the code in htaccess file.
And also how to remove the file extension from the URL. Down below is the code I tried
#Remove extension
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
And I tried below code which was generated from that website which is mentioned above
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/?(.*).(php)$ /$1 [R=301,L]
</IfModule>
How can I solve these two
Thank You in advance
The below code will remove php extention from the URL
RewriteEngine On
# for 404 redirection
ErrorDocument 404 https://www.yourdomain.in/404
# below code rewrites php extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
# Remove php extention
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
Hence the url https://www.yourdomain.in/404 will load https://www.yourdomain.in/404.php
Recently, I got some help modifying my .htaccess file to hide the .php extension in the url. After I got it working, a few bugs occurred with my site. For whatever reason, any php page in a sub directory returns a 404 error. For example:
mywebsite.com/test/test.php (this 404's)
but
mywebsite.com/test/test.html (this works just fine)
Since this only started occurring after my .htaccess file was modified, I am assuming the problem lies there. The file contains as follows:
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.mywebsite.com/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /.+?\.php [NC]
RewriteRule ^(.+?)\.php$ /$1 [R=301,L,NE]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
ErrorDocument 404 /404.php
DirectoryIndex index.php
If there is any more information I can provide to help solve this, please let me know!
Thank you for all the help.
Try these rules in your root .htaccess:
ErrorDocument 404 /404.php
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ /$1 [R=301,L,NE]
# Redirect external .php requests to extensionless url
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /.+?\.php [NC]
RewriteRule ^(.+?)\.php$ /$1 [R=301,L,NE]
# Resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
If you are intended to hide your PHP project structure to be accessed by public as directory tree ... you should consider adding a .htaccess file in the root of your project with just 1 line
Add the following line to your .htaccess file.
Options -Indexes
I have a rewrite condition in my .htaccess file which removes the need for .php file extension
RewriteEngine On
RewriteRule ^([^.]+)$ $1.php
so http://site.com/blog opens http://site.com/blog.php
but if old users type /blog.php it will also load the page
is there a way to prevent or redirect pages with .php or any other file extention to the one without it?
i mean if user entered /blog.php or /blog.asp it should either fail to load or redirect to /blog (without extention)
A better way to accomplish this would be to only rewrite if a .php by that name exists. Otherwise throw 404 for the original URL. The second set of rules would take care of removing the extension and avoiding the redirect loop.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{THE_REQUEST} ^(?:GET|POST)\ /.*\.php\ HTTP.*$ [NC]
RewriteRule ^(.*)\.php$ $1 [R=301,L]
RewriteEngine On
RewriteRule ^([^.]+)$ $1.php [L]
RewriteRule ^(.*?)\.php$ $1 [R=301,L]
You can use the rule
RewriteRule ^(.+)\.php$ $1 [R=301,L]
This would cause apache to send back a redirect to the browser which would update it's URL to the one stripped from the extension. But make sure to place this rule in front of the one the redirects to .php internally
Try this:
RewriteRule ^(.*?)\.([a-z0-9]+)$ $1 [R=301,L]
I m trying to redirect all pages with extension .php to .html
with the following code:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)\.php$ /rules/$1.html [R=301,L]
RewriteRule ^([^\.]+)\.html$ /rules/$1.php [NC,L]
but got an error "This webpage has a redirect loop"
You have done first redirection of .php to .html and after that you doing .html to .php redirect so, that will recursion of redirection.
make code like this. that will redirect .php to .html files.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)\.php$ /rules/$1.html [R=301,L]
Replace your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo.html
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[\s?] [NC]
RewriteRule ^ %1.html? [R=301,L]
# To internally forward /dir/foo.html to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)\.html?$ /$1.php [L,NC]
How can I never show index.php? For example, all requests to
site.com/index.php/controller are redirected in the browser address bar to
site.com/controller?
My current .htaccess removes index.php but when a user directly types site.com/index.php/controller they are still shown that address in the address bar as opposed to site.com/controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
NOTE: Before flaming, I have checked lots of .htaccess threads that solve redirecting index.php but I haven't found one to never show index.php. Here are a few...
Not Show index.php in subdirectory
remove index.php in codeigniter
Try adding the following to your .htaccess file in the root of your domain
RewriteEngine On
RewriteBase /
#if you get a request for this /index.php/controller
RewriteCond %{REQUEST_URI} ^/index.php/controller$
#redirect to just controller
RewriteRule . controller [R=301,L]
If you need this to work for any path_info use the rule below instead
RewriteEngine On
RewriteBase /
#if you get a request for this /index.php(any-path)
RewriteCond %{REQUEST_URI} ^/index.php/(.+)$
#redirect to any-path
RewriteRule . %1 [R=301,L]
Try:
RewriteEngine On
RewriteBase /
RewriteRule ^index.php(.*)% ^/$1 [QSA,R]
I have not tested this, but as I understand it, it will take an URL with index.php as the filename (regardless of anything following it) and redirect it to /(.*) (i.e. anything that followed index.php in the original request). The QSA flag appends the query string, and R makes it a hard Redirect rather than just rewriting the URL on the current request.
For anyone looking for a more generalized solution:
Options +FollowSymLinks -MultiViews
#Disallow listing contents of subfolders
Options All -Indexes
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# If index or index.php requested, strip and redirect
RewriteCond %{THE_REQUEST} index(\\.php)?
RewriteRule ^index(\\.php)?$ http://yourdomain.com/ [R=301,L]
## Part 1
## 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]
## Part 2 - handle incoming that lacks extension (except for existing folders)
## To redirect /dir/foo to /dir/foo.php, (skip for admin or user dirs)
RewriteRule ^(admin|user)($|/) - [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]