954,176 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

URL Rewrite in PHP

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.

zippee
Posting Whiz in Training
294 posts since Jan 2005
Reputation Points: 10
Solved Threads: 7
 

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 Fruit, a spider will not be able to follow this link since they cannot run client-side script.

Troy
Posting Whiz
362 posts since Jun 2005
Reputation Points: 36
Solved Threads: 6
 

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.

zippee
Posting Whiz in Training
294 posts since Jan 2005
Reputation Points: 10
Solved Threads: 7
 

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.

Troy
Posting Whiz
362 posts since Jun 2005
Reputation Points: 36
Solved Threads: 6
 

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

Zachery
The Geek Father
Team Colleague
894 posts since Nov 2003
Reputation Points: 96
Solved Threads: 21
 
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.

PoA
Posting Whiz in Training
237 posts since Jul 2004
Reputation Points: 26
Solved Threads: 9
 

An example of how to write a simple rewriting rule:

[URL sipped]

a-geek
Newbie Poster
1 post since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

An example of how to write a simple rewriting rule:

[URL snipped]


4 year bump! Drink!

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

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....

UNDiL
Newbie Poster
1 post since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

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

didier_m
Newbie Poster
20 posts since Aug 2010
Reputation Points: 10
Solved Threads: 2
 

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

SNIP

dsd_inc123
Newbie Poster
2 posts since Aug 2010
Reputation Points: 8
Solved Threads: 1
 

ok for mod_rewrite but what should be the rule?

RewriteRule ^* *.php

would this do the trick?

didier_m
Newbie Poster
20 posts since Aug 2010
Reputation Points: 10
Solved Threads: 2
 

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
sam-i-am
Newbie Poster
23 posts since Aug 2010
Reputation Points: 10
Solved Threads: 2
 

I Sam,

thanks for you solution, i'll certainly try it

didier

didier_m
Newbie Poster
20 posts since Aug 2010
Reputation Points: 10
Solved Threads: 2
 

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

didier_m
Newbie Poster
20 posts since Aug 2010
Reputation Points: 10
Solved Threads: 2
 

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.....

sam-i-am
Newbie Poster
23 posts since Aug 2010
Reputation Points: 10
Solved Threads: 2
 

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??

didier_m
Newbie Poster
20 posts since Aug 2010
Reputation Points: 10
Solved Threads: 2
 

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?

sam-i-am
Newbie Poster
23 posts since Aug 2010
Reputation Points: 10
Solved Threads: 2
 

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

didier_m
Newbie Poster
20 posts since Aug 2010
Reputation Points: 10
Solved Threads: 2
 

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...

sam-i-am
Newbie Poster
23 posts since Aug 2010
Reputation Points: 10
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You