Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect .php URLs to URLs without extension [duplicate]

Possible Duplicate:
Remove .php extension with .htaccess

I want to make all my website URLs, myurl/webpageexamples.php, to be renamed to its non-extension version myurl/webpageexamples, if possible in .htaccess and at the same time redirect all traffic from myurl/webpageexamples.php to the new myurl/webpageexamples.

I have found several to redirect from PHP to non-PHP like:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]

What I have:

myurl/webpageexamples.php

What I want:

myurl/webpageexamples

I would like to have the SEO score transferred to new myurl/webpageexamples.

like image 431
Gorrion Avatar asked Nov 04 '12 20:11

Gorrion


People also ask

How can I redirect a URL to another URL in PHP?

The header function in PHP can be used to redirect the user from one page to another. It is an in-built function that sends raw HTTP header to the destination (client). The 'header_value' in the function is used to store the header string. The 'replace_value' parameter stores the value that needs to be replaced.

How do I automatically redirect in PHP?

To set a permanent PHP redirect, you can use the status code 301. Because this code indicates an indefinite redirection, the browser automatically redirects the user using the old URL to the new page address.

How many ways I can redirect a PHP page?

Redirection from one page to another in PHP is commonly achieved using the following two ways: Using Header Function in PHP: The header() function is an inbuilt function in PHP which is used to send the raw HTTP (Hyper Text Transfer Protocol) header to the client.


1 Answers

You want to make sure you are redirecting if the actual request is for a php page, and internally rewriting when the URI is missing the php:

RewriteEngine On

# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
like image 107
Jon Lin Avatar answered Sep 24 '22 15:09

Jon Lin