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]
1

4 Answers 4

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]
Sign up to request clarification or add additional context in comments.

2 Comments

There's no need for the NC flag, because the patterns do not contain any case specific characters.
@OlafDietsche Thanks for input :)
1

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>

3 Comments

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?
on tag a solve problem but menu display time getting 404 error
please make sure that your file exist there and you are giving it the correct path.
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

1 Comment

Duplicating the non-working rules doesn't answer the question.
0
  # Remove .html-extension from url
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}\.html -f
  RewriteRule ^([^\.]+)$ $1.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.