2

I have a problem whereby google has indexed some pages with the wrong URL.

The URL they are indexing is:

http://www.example.com/user/emp.php

and HTML URL:

 http://www.example.com/login.html

I need it to redirect to:

http://www.example.com/user/emp

and HTML URL:

http://www.example.com/login

here is my .htaccess file

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Ankit
  • 120
  • 1
  • 10
  • The better way to do this: Let Google recrawl your site. Here is a good answer how to do this: http://stackoverflow.com/questions/9466360/how-to-request-google-to-re-crawl-my-website – Oliver Mar 20 '17 at 10:36

4 Answers4

4

First check, if there exists an appropriate target ending in .html or .php, then rewrite to that URL

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+)$ $1.html [L]

RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [L]
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45
2

Replace it with this code in your htacess file.

     RewriteEngine On
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteRule ^([^\.]+)$ $1.php [NC,L]

And please remove .html and .php from all your links which you are calling for example.

    <a href="index.html">Home</a>

     To

      <a href="index">Home</a>
Iqbal Butt
  • 196
  • 2
  • 15
  • Why removing .html from the link is actually confusing to me. the rewriting happens from the server side. How come that relate to removing .html from the link? – Shobi Mar 20 '17 at 10:43
  • on tag a solve problem but menu display time getting 404 error – Ankit Mar 20 '17 at 10:52
  • please make sure that your file exist there and you are giving it the correct path. – Iqbal Butt Mar 20 '17 at 10:53
0

Make sure you have the mod_rewrite module is installed, Check the output of phpinfo(); function to confirm. I think the following .htaccess is fine. Place the htaccess in the root of your application.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]

Read This on how to install mod_rewrite in php

Community
  • 1
  • 1
Shobi
  • 10,374
  • 6
  • 46
  • 82
0
  # Remove .html-extension from url
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}\.html -f
  RewriteRule ^([^\.]+)$ $1.html
Alfin Paul
  • 1,543
  • 14
  • 26