Hello everybody, I have canonical issue in my website. I want to redirect "ruraltraveller.com" and "www.ruraltraveller.com/index.php" to "www.ruraltraveller.com" so please suggest me how I will do this using .htaccess. If possible please give me code snippet for that as well. Thanks in advance.

Dani AI

Generated

Short summary: the goal is to canonicalize to the www host and strip explicit /index.php requests so search engines and users see a single URL. asked for exactly that; 's suggestion is on the right track and 's PHP header will work but is less efficient (server-level redirects are preferred).

A compact, safe .htaccess approach (test with temporary 302s first, then switch to 301):

RewriteEngine On
RewriteBase /

# 1) Redirect non‑www host to the canonical host (preserve path and query)
RewriteCond %{HTTP_HOST} !^www\.ruraltraveller\.com$ [NC]
RewriteRule ^(.*)$ http://www.ruraltraveller.com/$1 [R=302,L]

# 2) Redirect explicit browser requests for /index.php to the site root
#    THE_REQUEST ensures this only affects direct client requests (avoids loops).
RewriteCond %{THE_REQUEST} \s/index\.php[?\s] [NC]
RewriteRule ^index\.php$ http://www.ruraltraveller.com/ [R=302,L]

Notes and troubleshooting

  • Use the 302 (temporary) flags while testing so browsers and search engines don't cache the redirect; change both R=302 to R=301 when confirmed.
  • If the site runs on HTTPS, replace http:// with https:// (or combine host+scheme checks in one rule).
  • Backup the existing .htaccess before editing and ensure mod_rewrite is enabled.
  • To test quickly from the command line: curl -I -L http://ruraltraveller.com/index.php — look for the 302/301 headers and final Location.
  • Clear your browser cache when re-testing; 301s are aggressively cached by browsers and crawlers.
  • After implementing, update internal links, sitemap, and canonical <link> tags to the www root and check Search Console/crawl reports for any redirect chains.

Why this over the PHP header approach: server rewrites run earlier and are faster; the THE_REQUEST check avoids redirecting internally rewired requests and prevents redirect loops. Once working, follow up by converting to permanent 301s and removing duplicate internal links.

Recommended Answers

All 3 Replies

This should do it ... it's untested though.

 # Force everyone to use www.example.com
 RewriteCond        %{HTTP_HOST}    !^www.example.com$              [NC] 
 RewriteRule        ^(.*)$             [R=301,L] 

 RewriteRule ^index.php$ http://www.example.com [R=301,L]

This is the code...
<?
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.new-url.com" );
?>

Thnx...:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.