Is there any way I can convert dynamic URL into static URL in php? i.e. convert php file into html so that the page is search engine friendly. For example:

Change this: http://mysite.com/pagename.php
To this: http://mysite.com/pagename
or this: http://mysite.com/pagename.html

I know there is something called mod_rewrite in Apache, but not sure how it works and whether it's related to what I want.

Thanks for any comment.

Recommended Answers

All 25 Replies

Yes, mod_rewrite is what you want if you are using Apache.

If anybody has any proof, I'd like to see it, but the official word from Google and others is that data-driven content (.ASP, .PHP, .JSP, etc.) is not a problem for their spiders. The only problem for spiders associated with data-driven pages is speed. A spider obviously "browses" your site a lot faster than a human. It may request a dozen pages in a single second. Spiders are not as patient as most humans, either. If your page takes 5 seconds to return--whether static or data-driven, the spider may consider the link bad and move on.

My point is that jumping through hoops to make your data-driven pages LOOK like static pages doesn't help your pages load any faster--which is the real issue with search engines--at least Google anyway.

Google has a Facts & Fiction page here that explains they can index dynamic pages.
http://www.google.com/webmasters/facts.html

At least for google, querystrings are not an issue either, although they do recommend you keep your querystrings short with only a few parameters.

Another "secret" is that if the only way to get to your pages is via a search on your site, then a spider will never find them. You do need every page that you want indexed somehow linked from another page. For example, if your site sells fruit, and the only way for me to reach your fruit_detail.php?fruit=apple page is by searching your inventory for "apple", then a bot will not find this page. However, if I can click on "Inventory" in your menu, then click "Apples" on your page listing all the fruit you sell, a bot can follow these links, too. (This assumes your menu is not generated by javascript, does not require javascript to navigate, and is not a Flash movie menu.)

This is a huge topic, and should be discussed in http://www.daniweb.com/techtalkforums/forum45.html, but one last tip I can offer is to remember you need standard links to your content. If a link has a target like <a href="javascript:LoadPage('fruit.htm')">Fruit</a>, a spider will not be able to follow this link since they cannot run client-side script.

commented: Informative, with links to back it up. Excellent! +1

Troy, that's really helpful!

I realise the search engine can search through dynamic site, but not many people convince about this, especially those non-programmers. this is why I try to have html version to convince my clients.

I developed an ASP site for a client years ago. The site is still in use today. It is an ASP site using a SQL Server database. The site has only 5 ASP scripts that produce over 250 data-driven pages. So all the "page" URLs are similar to trip.asp?S=14&L=57. Although the pages load very fast, I read that search engines may not search ASP pages, so I wrote a powerful (but surprisingly simple) script that generates a static HTML site from the ASP site. It crawls the site like a spider and generates a static .htm version of every dynamic page. Now, about once a week, the client runs the "generate static site" utility to update the site with recent data changes. The result is that I've not seen any improvement in search engine ranking. The site was indexed before, just 100s of hits down in the results. This has not changed.

I know there is a lot more to SEO than static vs dynamic, but my point is that in my experience and from what I read from Google, static vs dynamic does not matter.

They "prefer" .html pages over dynamic pages, but Google doesn't care, they spider forums just fine, take a look at vBulletin.com for example, ALOT is indexed

Change this: http://mysite.com/pagename.php
To this: http://mysite.com/pagename
or this: http://mysite.com/pagename.html

http://mysite.com/pagename
http://mysite.com/pagename.html

Both will work in mod_rewrite.

As for http://mysite.com/pagename, try
RewriteRule ^pagename pagename.php

As for http://mysite.com/pagename.html, try
RewriteRule ^pagename.html pagename.php

There is an another option you can also try called php pathinfo. http://www.tutorio.com/tutorial/php-alternative-to-mod-rewrite-for-se-friendly-urls

Hope it helps.

An example of how to write a simple rewriting rule:

An example of how to write a simple rewriting rule:

4 year bump! Drink!

i understand that but can anyone tell me how i can get value on next page...
means... my real url was movile.php?movieid=$mid[0] but i have change this one to moviename.html with the help of url rewrite rule... but now tell me how can i get movieid on next page while no id is in new url....

http://mysite.com/pagename
http://mysite.com/pagename.html

Both will work in mod_rewrite.

As for http://mysite.com/pagename, try
RewriteRule ^pagename pagename.php

As for http://mysite.com/pagename.html, try
RewriteRule ^pagename.html pagename.php

There is an another option you can also try called php pathinfo. http://www.tutorio.com/tutorial/php-alternative-to-mod-rewrite-for-se-friendly-urls

Hope it helps.

May I ask : what if I want all my result pages url without extension?? What should I put as rule?
RewriteRule ^???? ?????.php

thanks

u can use a module_rewrite apache module for url rewriting...

SNIP

ok for mod_rewrite but what should be the rule?

RewriteRule ^* *.php

would this do the trick?

Is there any way I can convert dynamic URL into static URL in php? i.e. convert php file into html so that the page is search engine friendly. For example:

Change this: http://mysite.com/pagename.php
To this: http://mysite.com/pagename
or this: http://mysite.com/pagename.html

I know there is something called mod_rewrite in Apache, but not sure how it works and whether it's related to what I want.

Thanks for any comment.

lets say you have numerous file with .php extention and you want to rename them to .html
if this is the problem then the solution is:

RewriteEngine on
RewriteRule ^(.+)\.html$ $1.php

I Sam,

thanks for you solution, i'll certainly try it

didier

So I try it but the php extension don't be replaced by html in the url. Is this only true for non-static links?

I put the htacces in the dir where the page is but no changes and mod_rewite is on

have you tried renaming your http://bakwas.tk/abc.php to http://bakwas.tk/abc.html ?
well the renaming part should be done by php. where ever the link to the .php file appears you will need to manually change the file extension. try changing the above URL to the URL with .html extension hope it will work as it works on my system.

lets hope for the best....


let me know if it worked?

it should work.....

This is strange, I realy thought Apache will do the renaming. Is it realy the way to do it?

What is the meaning of "AllowOverride All"? maybe is this missing in my htaccess??

This is strange, I realy thought Apache will do the renaming. Is it realy the way to do it?

What is the meaning of "AllowOverride All"? maybe is this missing in my htaccess??

to know more about allowoverride all look in http://httpd.apache.org/docs/1.3/mod/core.html#allowoverride

well to enable url rewrite allowoverride must be set to all from its default value none.

hope your problem is solved then?

Hi Sam,

so, I did try it but it seems that AllowOverride is set to none by the hosting company and they don't want to change it in the configuration for security reason!!!! Is using AllowOverride really a security treath??

If it's stay "none", i think I would'nt be able to rewrite at all or should the rewriting in php do the trick?

thanks

Hi Sam,

so, I did try it but it seems that AllowOverride is set to none by the hosting company and they don't want to change it in the configuration for security reason!!!! Is using AllowOverride really a security treath??

If it's stay "none", i think I would'nt be able to rewrite at all or should the rewriting in php do the trick?

thanks

The AllowOverride flag tells the web server(Apache) that it should process the directive from the .htaccess file also. If it is off it will not process the .htaccess file and hence you will not be able to rewrite the url..

further every hosting company has different methods to change the default settings try contacting the hosting provider. they may change the server setting for your host. and not globally...

Hi Sam,
I'm leaving today for a week for my job, so I will continue testing when I'm back and certainly keep in touch..
thanks anyway for your patience

didier

Hi guys, I'm learning the mod-rewrite and I was using it for few months. Now, I want to use it in a different way. I hope you can help me with that or at least you can give me a hint.

Original Url:
http://www.domain.com/index.php?page=news&id=12

Current Url (with mod-rewrite):
http://www.domain.com/news-12

I'm using the following mod-rewrite for this link:

RewriteRule ^news-(.*)$ /index.php?page=news&id=$1 [L]

I would like to acheive the following url:
http://www.domain.com/news/12

and I already try the following RewriteRule:

RewriteRule ^news/(.*)$ /index.php?page=news&id=$1 [L]

The problem is, when it goes to this page, it will not load any css, image and even javascripts. The other problem is, when you try to access other pages (www.domain.com/contact/) the url become like this: http://www.domain.com/news/12/contact

Do you guys have any idea how is possible to fix this ?!

Hi guys, I'm learning the mod-rewrite and I was using it for few months. Now, I want to use it in a different way. I hope you can help me with that or at least you can give me a hint.

Original Url:
http://www.domain.com/index.php?page=news&id=12

Current Url (with mod-rewrite):
http://www.domain.com/news-12

I'm using the following mod-rewrite for this link:

RewriteRule ^news-(.*)$ /index.php?page=news&id=$1 [L]

I would like to acheive the following url:
http://www.domain.com/news/12

and I already try the following RewriteRule:

RewriteRule ^news/(.*)$ /index.php?page=news&id=$1 [L]

The problem is, when it goes to this page, it will not load any css, image and even javascripts. The other problem is, when you try to access other pages (www.domain.com/contact/) the url become like this: http://www.domain.com/news/12/contact

Do you guys have any idea how is possible to fix this ?!

the problem with your second rewrite rule is that you haven't escaped the "/" every thing in your is fine.

try

RewriteRule ^news\/(.*)$ /index.php?page=news&id=$1 [L]

for js not being loaded i think that the path is wrong try using js path like
/js/a.js

where / is the server root

I tryed your code for RewriteRule, but I get the same result. I'm guessing that maybe the problem is with my url replacement function which I used to change php urls.
I use the following function with ob_get_contents() to replace the urls.

function replace_for_mod_rewrite(&$s) {
	$urlin = array(
		"'(?<!/)index.php\?page=news&amp;id=(.*)'",
		"'(?<!/)index.php\?page=profile&amp;user=(.*)'",
		"'(?<!/)index.php\?page=contact'"
	);
	$urlout = array(
		"news/\\1",
		"profile/\\1",
		"contact/"
	);
$s = preg_replace($urlin, $urlout, $s);
return $s;
}

and for Javascript and css problem, I think something is missing in my mod_rewrite. I'm using partical path for my css and javascript. So, when the page is trying to access the css or javascript, the file is in parent folder.

url: http://www.domain.com/contact/
css: http://www.domain.com/css/style.css

So, it will try to access http://www.domain.com/contact/css/style.css which is not exist in /contact folder.

I have

RewriteBase /

in my .htaccess file. but I think there must be something to tell the rewrite module that it must load the files from base directory.

I'm searching the internet for a long time to find the answer and maybe a small sample code. But, as you may know, all the website have the tutorials about how to hide .php extention and simple stuff about .htaccess and mod_rewrite.

Regards

i understand that but can anyone tell me how i can get value on next page...
means... my real url was movile.php?movieid=$mid[0] but i have change this one to moviename.html with the help of url rewrite rule... but now tell me how can i get movieid on next page while no id is in new url....

Hi,
you can use the parameter in the url like:
www.example.com/movies/11/
then convert the 11 to parameter in the urlrewite

RewriteRule ^movies/([0-9]+)/?$ movile.php?movieid=$1

we can convert .php file into .html file through .htaccess file.
RewriteEngine on
RewriteRule ^(.*).html$ $1.php [nc]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.