Page 1 of 1

URL rewrite using PHP without using .htaccess

Posted: Fri Sep 18, 2009 1:26 pm
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.

Re: URL rewrite using PHP without using .htaccess

Posted: Fri Sep 18, 2009 1:48 pm
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.

Re: URL rewrite using PHP without using .htaccess

Posted: Fri Sep 18, 2009 1:49 pm
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]

Re: URL rewrite using PHP without using .htaccess

Posted: Fri Sep 18, 2009 1:50 pm
by Inkyskin
Ah, so I was wrong lol, but close!