I am having a redirect issue. I have my htaccess setup to do the following...
1. Remove PHP extensions from URL
2. Add custom error pages
3. Force some 301 redirects
But there is a huge problem with the redirects...
If a user goes to ...
www.mysite.com/articles/jewelry/types.php
... then, based on the 2nd 301 redirect, they should be sent to ...
www.mysite.com/article/medieval-jewelry-types-and-functions/
... however, they are redirected to ...
www.mysite.com/article/medieval-jewelry/types
If they go to ...
www.mysite.com/articles/picks/A_1.php
... they should be redirected to...
www.mysite.com/article/picks-tools/
... but they get sent to ...
www.mysite.com/article/how-to-pick/
It looks like the redirect is changing PART of the url ... probably because of the .php extension (but that is just speculation - because I know very little about editing the htaccess file). Any ideas?
Below is most of my htaccess file, with some comments on what MIGHT be causing the issue. Any idea how to fix it?
# enable the rewrite engine
RewriteEngine On
# Set your root directory
RewriteBase /
# remove the .php extension
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index and reference the directory
RewriteRule (.*)/index$ $1/ [R=301]
# remove trailing slash if not a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# forward request to html file, **but don't redirect (bot friendly)**
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
ErrorDocument 400 /400.php
ErrorDocument 401 /401.php
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
ErrorDocument 405 /405.php
ErrorDocument 408 /408.php
ErrorDocument 414 /414.php
ErrorDocument 500 /500.php
ErrorDocument 502 /502.php
ErrorDocument 504 /504.php
# example of redirects
Redirect 301 /articles/index.php /articles/
# it looks like the redirect below is conflicting with...
Redirect 301 /articles/jewelry/ /article/medieval-jewelry/
# ... this redirect.
Redirect 301 /articles/jewelry/types.php /article/medieval-jewelry-types-and-functions/
# And this redirect...
Redirect 301 /articles/picks/ /article/how-to-pick/
Redirect 301 /articles/picks/index.php /article/how-to-pick/
# ... is conflicting with this one...
Redirect 301 /articles/picks/A_1.php /article/picks-tools/
Problem seems to be wrong order of rules, incorrect regex and mixing of mod_rewrite rules with Redirect (mod_alias) rules. Try this:
ErrorDocument 400 /400.php
ErrorDocument 401 /401.php
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
ErrorDocument 405 /405.php
ErrorDocument 408 /408.php
ErrorDocument 414 /414.php
ErrorDocument 500 /500.php
ErrorDocument 502 /502.php
ErrorDocument 504 /504.php
# enable the rewrite engine
RewriteEngine On
# Set your root directory
RewriteBase /
# example of redirects
RewriteRule ^articles/index\.php$ /articles/ [L,NC,R=302]
# it looks like the redirect below is conflicting with...
RewriteRule ^articles/jewelry/?$ /article/medieval-jewelry/ [L,NC,R=302]
RewriteRule ^articles/jewelry/types\.php$ /article/medieval-jewelry-types-and-functions/ [L,NC,R=302]
RewriteRule ^articles/picks/?$ /article/how-to-pick/ [L,NC,R=302]
RewriteRule ^articles/picks/index\.php$ /article/how-to-pick/ [L,NC,R=302]
RewriteRule ^articles/picks/A_1\.php$ /article/picks-tools/ [L,NC,R=302]
# remove the .php extension
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index and reference the directory
RewriteRule (.*)/index$ $1/ [R=301]
# remove trailing slash if not a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# forward request to php file, **but don't redirect (bot friendly)**
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
Related
I am trying to add an ErrorDocument (page not found) to my website.
I have the errorpage.php
<?php
http_response_code(404);
?>
...
<h1>page not found</h1>
And my .htaccess looks like this:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /$1.html [QSA,L]
Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
ErrorDocument 404 http://www.example.com/errorpage.php
ErrorDocument 500 http://www.example.com/errorpage.php
If I open a not existing page like: "www.example.com/xxx" I get redirected to: "www.example.com/errorpage.php" but with the status code:
302 found
404 not found
and this is bad I only need the status code "404 not found".
Why do I get two status codes and the redirect from found to not found?
Maybe your request is non www and first redirect 302 to www and then get http404 status from errorpage.php, remove host and check. You can also move this lines to top of your .htaccess file. Maybe it help
ErrorDocument 404 /errorpage.php
ErrorDocument 500 /errorpage.php
I had to add another RewriteCond:
RewriteCond %{REQUEST_URI} .!errorpage\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /$1.html [QSA,L]
ErrorDocument 404 /errorpage.php
ErrorDocument 500 /errorpage.php
I have the following codes in my htaccess file which removes .php extension from page name and redirect to a 404 page when a trailing slash is inserted at the end of a page name in the following directories.
example.com/file [redirecting]
example.com/file/ [not redirecting]
example.com/folder/file [redirecting]
example.com/folder/file/ [not redirecting]
example.com/folder/folder/file [not redirecting]
example.com/folder/folder/file/ [not redirecting]
I have tried several htaccess codes but 5. doesn't redirects. I will like 5. to be redirecting like 1. and 3.
CODE
# error pages
ErrorDocument 203 /error/203.php
ErrorDocument 204 /error/204.php
ErrorDocument 301 /error/301.php
ErrorDocument 400 /error/400.php
ErrorDocument 401 /error/401.php
ErrorDocument 403 /error/403.php
ErrorDocument 404 /error/404.php
ErrorDocument 405 /error/405.php
ErrorDocument 406 /error/406.php
ErrorDocument 500 /error/500.php
ErrorDocument 502 /error/503.php
ErrorDocument 503 /error/503.php
ErrorDocument 504 /error/504.php
Options -Indexes
RewriteEngine on
# remove .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^.]+)$ $1.php [NC,L]
# rewrite index page
RewriteRule ^login index.php [NC,L]
# rewrite profile page
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
# site.com/profile.php
RewriteRule ^([a-zA-Z0-9_.]+)$ profile.php?tg_u=$1 [NC,L]
#RewriteRule ^([^.]+)$ $1.php [NC,L]
# block access to htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
How to redirect 404 , in htaccess I have add this
ErrorDocument 404 http://example.com/404/
ErrorDocument 500 http://example.com/500/
# or map them to one error document:
# ErrorDocument 404 /pages/errors/error_redirect.php
# ErrorDocument 500 /pages/errors/error_redirect.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /pages/errors/404.php [L]
RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /pages/errors/500.php [L]
# or map them to one error document:
#RewriteCond %{REQUEST_URI} ^/404/$ [OR]
#RewriteCond %{REQUEST_URI} ^/500/$
#RewriteRule ^(.*)$ /pages/errors/error_redirect.php [L]
It has reduce but they are increasing, is there a way how can I redirect properly or maybe I'm mising something. How can I fix all of them ?
Thanks
You dont need to use RewriteRules. You can simply use an absolute path instead of using full url as your errordocument target :
ErrorDocument 404 /pages/errors/404.php
So, I want to rewrite my url from this:
http://example.com/l1
to
http://example.com/?loc=l1
Currently I have this:
RewriteEngine On
RewriteRule ^/(.*)$ ./?loc=$1 [NC,L]
ErrorDocument 404 /errors/404.php
But each time I type http://example.com/l1 it gives me a 404 error! Please help!
Use this .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /?loc=$1 [NC,L]
ErrorDocument 404 /errors/404.php
I do not rewrite the links to existing files with !-f.
And URI in RewriteRule does not begin with a /
I have recently setup a index.php redirect through .htaccess. The idea here is to negate duplicate content issue that crops up when a site has both an index.php and / (homapage) getting indexed.
Here is my original .htaccess
Options -Indexes
ErrorDocument 403 /customerrors/403.html
ErrorDocument 401 /customerrors/401.html
ErrorDocument 400 /customerrors/400.html
ErrorDocument 500 /customerrors/500.html
ErrorDocument 404 /customerrors/404.html
Pretty basic.
I used the technique listed here to redirect the index.php to /.
http://www.askapache.com/htaccess/redirect-index-blog-root.html
It works great too. One issue is, it breaks the 404 pages.
This is the modified .htaccess that is breaking the 404 pages.
RewriteEngine On
RewriteBase /
DirectoryIndex index.php
RewriteCond %{http_host} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://example.com/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Options -Indexes
ErrorDocument 403 /customerrors/403.html
ErrorDocument 401 /customerrors/401.html
ErrorDocument 400 /customerrors/400.html
ErrorDocument 500 /customerrors/500.html
ErrorDocument 404 /customerrors/404.html
So if a user types in or goes to www.example.com/dafjkadbfda instead of serving 404 page, the URL remains the same(in this case the broken one) and severs the index.php page.
This in turn is opening another can of worms. All those broken pages are coming up as duplicate content and meta.
Is there another way to write the .htacess redirect that will take into account the 404 pages? Seems like that is the conflict here.
Thanks in advance.
This part of the .htaccess is the one that "breaks" the 404:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
You could manage the problem by sending a 404 error from your PHP script:
if ($not_a_valid_page){
header("$_SERVER[SERVER_PROTOCOL] 404 Not Found");
readfile("./customerrors/404.html");
die();
}