We are getting a lot of 404 errors on a Real Estate website after houses are sold and the listing goes offline. I am trying, via htaccess, to redirect the missing pages that Google Search Console shows as 404 to a home listing search page. I have tried the code below but it is redirecting all listing pages not just the ones that no longer exist. Not sure if it's my code or because the pages are dynamically created.
All the home listings are under www.example.com/homes-for-sale-details/[address]. If the listing no longer exists I want the page to redirect to www.example.com/homes-for-sale-details.
My htaccess code
# Redirect old home listing to a search page
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^homes-for-sale-details/(.*)$ https://www.example.com/homes-for-sale-details [L,NC,R=301]
</IfModule>
Where are I going wrong?
Many Thanks!
Edit (added more of htaccess code):
####################################
# START Redirect pages from old site
#
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^Properties https://www.example.com/home-listings [L,NC,NE,R=301]
RewriteRule ^Access https://www.example.com/search-homes [L,NC,NE,R=301]
RewriteRule ^Neighboorhoods https://www.example.com/neighborhoods [L,NC,NE,R=301]
RewriteRule ^Buyer-Resources https://www.example.com/buy-home-in-colorado-springs [L,NC,NE,R=301]
RewriteRule ^Relocation-Guide https://www.example.com/buy-home-in-colorado-springs/relocation-guide [L,NC,NE,R=301]
RewriteRule ^Buyer-Resources/Buyer-Finance/Finance-Information https://www.example.com/buy-home-in-colorado-springs/home-finance [L,NC,NE,R=301]
RewriteRule ^Seller-Resources https://www.example.com/sell-colorado-springs-home [L,NC,NE,R=301]
RewriteRule ^Area-Schools https://www.example.com/local-lifestyle/area-schools [L,NC,NE,R=301]
RewriteRule ^Colorado-Springs-Attractions https://www.example.com/local-lifestyle/colorado-springs-attractions [L,NC,NE,R=301]
RewriteRule ^Military-Bases https://www.example.com/local-lifestyle/military-bases [L,NC,NE,R=301]
RewriteRule ^About$ https://www.example.com/about-us [L,NC,NE,R=301]
RewriteRule ^contact$ https://www.example.com/contact-us [L,NC,NE,R=301]
RewriteRule ^Terms-Of-Service https://www.example.com/terms-of-service [L,NE,R=301]
RewriteRule ^Privacy-Policy https://www.example.com/privacy-policy [L,NE,R=301]
RewriteRule ^Site-Map https://www.example.com/sitemap [L,NC,NE,R=301]
RewriteRule ^neighborhoods/fountain$ https://www.example.com/neighborhoods/fountain-security-widefield [L,NC,NE,R=301]
RewriteRule ^neighborhoods/securitywidefield https://www.example.com/neighborhoods/fountain-security-widefield [L,NC,NE,R=301]
RewriteRule ^park-avenue-properties-blog https://www.example.com/blog [L,NC,NE,R=301]
RewriteRule ^Primary-Factors-the-Affect-the-Real-Estate-Market https://www.example.com/primary-factors-affect-real-estate-market [L,NC,NE,R=301]
</IfModule>
# END Redirect pages from old site
# Force HTTPS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
</IfModule>
# Remove "Blog" from blog post URLs and preserve blog paging
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !page
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^Blog/(.*)$ /$1 [L,NC,R=301]
</IfModule>
# Redirect old home listing to a search page
#<IfModule mod_rewrite.c>
# RewriteEngine On
# RewriteBase /
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteRule ^homes-for-sale-details/(.*)$ https://www.example.com/homes-for-sale-details [L,NC,R=301]
#</IfModule>
####################################
# Browser caching code removed :)
####################################
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I've tried to get this to work with the .htaccess file and could not. The issue with your script above is that you're not checking first for the 404 error, so all requests from that "folder" are being redirected. I've tried using the ErrorDocument directive in the .htaccess file with no luck, so the solution I worked out was to just use what wordpress already provides. In your themes subfolder (/wp-content/themes/THEMENAME) there should be a 404.php file. If it's not there you can create it. You can use some code at the top of this file to parse the request url and redirect the page. Something like this should work:
<?php
if (strpos($_SERVER['REQUEST_URI'], 'homes-for-sale-details') !== false)
header('Location: /homes-for-sale-details', true, 301);
else {
?>
// ... paste the current 404.php content here
<?php } ?>
Just remember to back up your current 404.php page in case you overwrite something you didn't mean to. If you want to try to get this working with the ErrorDocument stuff in htaccess be my guest, but for whatever reason, it wasn't working for me. Here's a post that shows how to do that.
Good luck!
After unsuccessfully trying to find a htaccess answer slim's answer got me thinging and I found this post WordPress Template Redirect (thank you so much for your help slim!!!)
Here is the modification I made to that code.
// Redirect missing home listing to a search page.
function __404_template_redirect()
{
if( is_404() )
{
$req = $_SERVER['REQUEST_URI'];
if ( is_file( $req )) {
return; // don't reduce perf by redirecting files
}
// check if "homes-for-sale-details" is in the URL
if ( strpos($req, 'homes-for-sale-details') == false ) {
return; // only redirect missing homes
}
// pull the parent directory and convert to site url
$parent_url = get_permalink( 1232 );
// redirect to parent directory
wp_redirect( $parent_url, 301 );
exit();
}
}
add_action( 'template_redirect', '__404_template_redirect' );
I am trying to get a few .htaccess rules converted to NGINX, but it's not going very well. I have to crawl here and beg for assistance :(~ Paypal-beer to anyone who solves it.
I have tried some online converters, but the code looks messy, and I don't quite understand what it's doing. The rules I would like to have converted:
# Rewrite any calls to html|json|xml|atom|rss
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+)\.(html|json|xml|atom|rss)$ $1/ [L]
# Rewrite any calls to /render to the image resizer
RewriteCond %{REQUEST_URI} render/
RewriteRule ^render/. app/parsers/slir/ [L]
# Rewrite routes to application index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ index.php?/$1/ [L,QSA]
Appreciate any help.
I believe it is sorted. Inside a NGINX server {} block, I included the following:
location / {
if (!-e $request_filename){
# Rewrite any calls to html|json|xml|atom|rss if a folder matching * exists
rewrite (.+)\.(html|json|xml|atom|rss)$ $1/ last;
# Rewrite any calls to /render to the X3 image resizer
rewrite ^/render/. /app/parsers/slir/ last;
# Rewrite routes to X3 application index.php if they are non-existent files/dirs
rewrite ^(.*)$ /index.php?$1 last;
}
}
https://gist.github.com/mjau-mjau/6dc1948284c90d167f51f1e566a8457b
I'm brand new to nginx and I am trying to convert the app I wrote over from Apache as I need the ability to serve a lot of clients at once without a lot of overhead!
I'm getting the hang of setting up nginx and FPM/FastCGI PHP but I can't wrap my head around nginx's rewrite format just yet. I know you have to write some simple script that goes in the server {} block in the nginx config but I'm not yet familiar with the syntax.
Could anyone with experience with both Apache and nginx help me convert this to nginx format? Thanks in advance!
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^school\.dev$ [NC]
RewriteRule ^(.*)$ http://www.school.dev/$1 [R=301,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?%{QUERY_STRING} [NE,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
</IfModule>
Here is the nginx conversion for your htaccess.
# nginx configuration
location / {
if ($http_host ~* "^school\.dev$"){
rewrite ^(.*)$ http://www.school.dev/$1 redirect;
}
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?$query_string break;
}
}
Question is about translating RazorCMS (razorcms.co.uk) rewrite rule but I think answer can be general.
I have two .htacces files. First is in main directory (/). I translated:
RewriteRule ^(.*)$ index.php?path=$1 [L,QSA]
to
location / { try_files $uri $uri/ #rewrite; }
location #rewrite { rewrite ^/(.*)$ /index.php?path=$1; }
There is no problem with this one.
Second .htacces file is in subdirectory (/rars).
Content inside is:
RewriteRule ^login/u/(.*)/p/(.*)$ index.php?login=1&u=$1&p=$2 [L,QSA]
RewriteRule ^login index.php?login=1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?path=$1 [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L,QSA]
I tried to translate it to different rules:
location /login { rewrite ^/login/u/(.*)/p/(.*)$ /index.php?login=1&u=$1&p=$2 break; }
location /login { rewrite ^/login /index.php?login=1 break; }
location /rars { if (!-e $request_filename){ rewrite ^(.*)$ /index.php?path=$1; } }
...but it doesn't work.
How can I translate .htaccess rules to Nginx rewrites if second .htaccess is in subdirectory? Can you point me in right direction?
Nginx doesn't have per directory contexts, so you need to give the full path in the location. Try:
location /rars/login { rewrite ^/rars/login/u/(.*)/p/(.*)$ /rars/index.php?login=1&u=$1&p=$2 break; }
location /rars/login { rewrite ^/rars/login$ /rars/index.php?login=1 break; }
location /rars { if (!-e $request_filename){ rewrite ^/rars/(.*)$ /rars/index.php?path=$1; } }
I'm trying to change the way pagination looks like, from: example.com/category?page=1 to example.com/category/page1/
For these purpose in .htaccess I was using this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((.*)/page([0-9]+)/)$ index.php?q=$2&page=$3 [L,QSA]
But I don't understand how to do it in nginx, I tried this:
location / {
if (!-e $request_filename){
rewrite ^/((.*)/page([0-9]+)/)$ /index.php?q=$2&page=$3 break;
}
}
And it doesn't work, it just breaks everything (getting 404 at categories)
Just use this
location / {
if (!-e $request_filename){
rewrite ^/((.*)/page([0-9]+)/)$ /index.php;
}
}