URL rewrite using PHP without using .htaccess

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
User avatar
mrvijayakumar
Forum Commoner
Posts: 58
Joined: Tue Aug 18, 2009 12:39 am
Location: Chennai city, India
Contact:

URL rewrite using PHP without using .htaccess

Post by mrvijayakumar »

Hi all,
I want to create SEO friendly URL's using pure PHP without using htaccess. How i can do it?

For example: http://www.example.com/profile.html, while i give this URL, it should show the content of http://www.example.com/profile.php.

Any idea about this issue? please share with me. Thanks in advance.
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Re: URL rewrite using PHP without using .htaccess

Post by Inkyskin »

Not definate, but I'm pretty sure you can add the .htaccess handler to the php ini file (AddHandler x-httpd-php .htm .html)

Do .html have any seo gains over .php anyway? I can understand http://www.example.com/profile/, however you really would need htaccess for that to work best.
User avatar
volomike
Forum Regular
Posts: 633
Joined: Wed Jan 16, 2008 9:04 am
Location: Myrtle Beach, South Carolina, USA

Re: URL rewrite using PHP without using .htaccess

Post by volomike »

Not possible if this is Apache. You'd either have to add the .htaccess file, or edit the default settings of the Apache webserver. The idea being that you'd add this in:

<FilesMatch "\.html">
AddType application/x-httpd-php .php .html
</FilesMatch>

Or you'd have to hook the 404 logic such that it goes to the .php file, but even that is not recommended and creates a lot of problems.

Another approach is to turn on MultiViews on the Apache web server and then instead of ending with .html, just leave the file extension off entirely, such as:

http://example.com/profile

The MultiView feature I think will then fire profile.php for you if I'm not mistaken.

However, some experienced Linux sysops consider that a security hole and dislike MultiViews.

No matter what, if this is Apache, the way to do this the right way is to use the .htaccess feature. The way I do it with .htaccess is like so:

SetEnv URL_PREFIX /mysite
# or "SetEnv URL_PREFIX /" if path to index.php are run off the domain name and not a subfolder

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . %{ENV:URL_PREFIX}index.php [QSA]
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Re: URL rewrite using PHP without using .htaccess

Post by Inkyskin »

Ah, so I was wrong lol, but close!
Post Reply