PDA

View Full Version : MOD Rewrite



nikomou
10-16-2005, 03:18 PM
Hey, I know its possible to rewrite php files to html, but how would you go about that? mod_rewrite?

I have 2 simple types of urls..

the 1st is just,

www.mysite.com/this-folder/this-php-page.php

I want that to be changed to

www.mysite.com/this-folder/this-php-page.htm

the other type of page is:

www.mysite.com/this-folder/this-php-page.php?network=orange

and I want that to be changed to

www.mysite.com/this-folder/this-php-page-orange.htm

Any Ideas / pushes to the right direction?

mwinter
10-17-2005, 11:34 PM
Hey, I know its possible to rewrite php files to html, but how would you go about that? mod_rewrite?I, for one, am not entirely sure what you're asking for here. You mention URLs later in your post, but above you're talking about files.

The rewrite Apache module (mod_rewrite) allows a URL to be changed at request time. This may either lead to a second request from a client (a server-side redirect), or Apache might deal with the changes internally.


www.mysite.com/this-folder/this-php-page.php

I want that to be changed to

www.mysite.com/this-folder/this-php-page.htmWhat exactly do you expect to happen with this change? That the server still parses a file with a .htm extension as a PHP file? Well preferably, you should avoid file extensions altogether, and use URLs like

  http://www.example.com/some-resource

You can then use content negotiation (http://httpd.apache.org/docs/2.0/content-negotiation.html) - either MultiViews or a type-map file - to respond with the appropriate resource.

By the way, the .htm extension is out-dated. It was only introduced by Microsoft because their early file systems couldn't handle extensions longer than three characters. As file names on a remote Web server have no relation to a user's file system, this is irrelevant and you should use the proper .html extension instead.


www.mysite.com/this-folder/this-php-page.php?network=orange

and I want that to be changed to

www.mysite.com/this-folder/this-php-page-orange.htmThat style of manipulation - query string into file name - would need mod_rewrite, but the extension is still irrelevant.



RewriteEngine on
RewriteBase path
RewriteCond %{QUERY_STRING} ^(.+&)?network=([^&]+)(?:&(.*))?$
RewriteRule ([^.]+)\.php $1-%2.html?%1%3
This snippet could be used in a .htaccess file to do what you described. The rewrite action would be internal, so the user would think they were going to the PHP file. You'd need to replace path with an appropriate prefix. For instance, in the directory, /deep/directory/, you'd specify /deep/directory.



RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.+&)?network=([^&]+)(?:&(.*))?$
RewriteRule (.*/)([^.]+)\.php $1$2-%2.html?%1%3 [R]
This is a server- or virtual-host-wide rule. Unlike the previous one, this triggers an external redirect so the user would type or navigate to an address containing an .php extension, but would see an .html extension.

If you do want your server to treat .html (or .htm) files as PHP files, then you need to alter its main configuration (though you could use a .htaccess file or mod_rewrite). However, it would then treat all files with that extension as PHP files, whether they contained <?php ... ?> blocks or not (unless you started adding Directory directives to limit the scope).

Mike

nikomou
11-10-2005, 12:59 PM
Ok, the scenario has changed slightly now...
my URL's will now look like something this:

http://www.mywebsite.com/handsets.php?handset=nokia-7610
http://www.mywebsite.com/handsets.php?handset=nokia-7610&tariff=orange

As you can see these are very messy urls., so I want to change these to urls that look like:

http://www.mywebsite.com/nokia-7610.html
http://www.mywebsite.com/nokia-7610-orange.html

Update:

Ok, i am now using the following code for my .htaccess file, but there's a few things wrong..


RewriteEngine On
RewriteRule (.*)-(.*).html handsets.php?hs=$1&tariff=$2

OK, this works great IF there isnt a - (dash " - ") after the &tariff in the url...

e.g. http://www.mysite.com/handsets.php?hs=nokia-7610&tariff=t-mobile. I could get rid of the dash (so it becomes &tariff=tmobile) but i want the rewrited url to have the dash in it. e.g. nokia-7610-t-mobile.html

ALSO - if there isnt a tariff specified (handsets.php?hs=nokia-7610&tariff=) i have to place a dash " - " after the rewriten page (nokia-7610-.html)

Anyone got any ideas?

tech_support
05-08-2007, 09:28 AM
Please create a new thread. Post moved to here (http://www.dynamicdrive.com/forums/showthread.php?t=20519)