homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Website
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Rewrite .html to .php, redirect php to html
Using mod_rewrite

 8:31 am on May 30, 2009 (gmt 0)


In start sorry for my english because its not my home language ...
Ok I saw many of your post same to my problem but till now did not get any of the one fixed on my problem.

I have a web which is dynamically designed but now the prob is that when I want these dynamic URLs to be redirected to Static URLs becz I heard that google and othe search engines crawls static URLs more efficiently then dynamic.

I saw some of your post and made some of the code to do this

that is :

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)\.html$ $1 [L]

I try to use some conditions like RewriteCond %{the_request} but can not able to work on it and can not understand it also

I open a anyname.php page with anyname.html then it works but when I open anyname.php then it is not redirect it to anyname.html and I want to do it becz then google can crawl my ranking pages as static URLs and delete all of mine Dynamic URLs from its database....

So can you please help me Jim?

Thanks in advance



 9:43 am on May 30, 2009 (gmt 0)

You need to clearly show the format and example of a URL used out on the web and show the format of the names of the file on the server.

As for the URLs that users 'see' and 'use' on the web, those are defined by the URLs in the links on your pages.

If you are changing to new URLs, you need a redirect from the old format to the new format.

If the old format was "dynamic" then you also need a rewrite to connect the new URL requests with the old format internal server filepath.


RewriteRule ^(.*)\.html$ $1 [L]

As coded, this takes a URL request for example.com/<something>.html and performs a rewrite such that the server looks for a file called /something (no extension) on the server to fulfill that request.

In this case, the code is exactly backwards if you intended to be using that for the rewrite; and that code likely does not do what you want for the internal filepath needing to have parameters within.

In this case, the code is incomplete if you intended that to be the redirect. The addition of the domain name to the target, and the [R=301,L] flag on the end, would start to get you in the right direction.

There was a question just yesterday covering the same issues, and listing all the steps you need to take.


 9:58 am on May 30, 2009 (gmt 0)


I am just finding some so feel nice to talk you ... lets come to point

I have dynamic pages like this www.mysite.com/category/pagename.php

I want to is when user or GBOT goes to www.mysite.com/category/pagename.php then redirect to the page www.mysite.com/category/pagename.html

and only the way to visit a page it through a static URL like www.mysite.com/category/pagename.html

so can you please explain me I do my work till this query now

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)\.html$ $1 [L]


 11:59 am on May 30, 2009 (gmt 0)

You need two rules, one for each of your stated requirements.
1) I want these dynamic URLs like www.example.com/category/pagename.php to be redirected to Static URLs
2) I open a anyname.php page with anyname.html

# Externally redirect direct client requests for /category/<pagename>.php
# to www.example.com/category/<pagename>.html
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /category/([^.]+)\.php[?]?[^\ ]*\ HTTP/
RewriteRule ^category/([^.]+)\.php$ http://www.example.com/$1.html? [R=301,L]
# Internally rewrite requests for /category/<pagename>.html to /category/<pagename>.php
RewriteRule ^category/([^.]+)\.html$ /category/$1.php$ [L]

Important: You must also change your PHP code to generate links to /category/<pagename>.html instead of linking to /category/ <pagename>.php on your HTML pages. Do not try to use the code above until you have done so. PHP's preg_replace function is often useful for this task.

As shown, this code applies to /category pages only, as stated in requirement #1. If you want it to apply to all pages, then remove "/category" from all lines.

See the thread Changing Dynamic URLs to Static URLs [webmasterworld.com] in our Apache Forum Library for a full description of this three-step process.



 12:17 pm on May 30, 2009 (gmt 0)

There's the opportunity to do other things here:

- use AddHandler to parse .html files for PHP scripts inside them - avoiding any need for redirects or rewrites at all.

Going extensionless (which would use the code above, except that the URL would NOT include .html on the end). The filenames on the server could be .html or .php, it wouldn't matter (but AddHandler would be needed if the files on the server were .html named).

Do any of your URLs use parameters? Those pass through your redirects and rewrites untouched in the code examples above.


 12:32 pm on May 30, 2009 (gmt 0)


Thanks for your reply

When I put these code I change my category name to funny like www.mysite.com/funny/pagename.php

but it does not work when I open a page www.mysite.com/funny/pagename.php it just opens, nor redirect to www.mysite.com/funny/pagename.html and neither works when I put the URL in browser tab like www.mysite.com/funny/pagename.html

Please give me what is happening here and what all this logic is please I be very thank full to you

Waiting for reply


 12:41 pm on May 30, 2009 (gmt 0)

Did you edit the word "category" to be the real category name?

Where did you put this code?

Is it in the .htaccess file in the root or is it in the .htaccess file in the /category/ itself?

The code will need to be modified if it is not placed in the root folder.


 2:24 pm on May 30, 2009 (gmt 0)

I place the actual category name in my root folder and I place .htaccess file in the root folder not in the category folder it self but it is not showing of the result .

I try to make one query my self:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)\.html$ $1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ (.+)\.php\ HTTP/
RewriteRule ^(.+)\.php$ /Copy%20of%20New%20Fun/$1\.html [R=301,L]

It do both of my work but not in effiecient manner mean that when opening anypage.php then redirect to anypage.html but I think there are some cautions in it mean that it will be dangerous to execute can you please let me know by checking this query that it is right or do some extra works


 2:35 pm on May 30, 2009 (gmt 0)

If you must use spaces (bad idea) in your URLs, then you must escape those spaces and use the [NE] flag on the rule:

RewriteRule ^(.+)\.php$ /Copy\%20of\%20New\%20Fun/1$.html [NE,R=301,L]

Do not escape the literal period preceding "html". It is not generally necessary to escape characters in the substitution URL except for "%" and "$".

As you've ignored my other suggested changes, I cannot comment further.



 2:39 pm on May 30, 2009 (gmt 0)

Don't use spaces or underscores in URLs. Use hyphens or something else instead.

As for the rest of the code, it still has errors in it that you need to correct using Jim's instructions above.

List redirects before rewrites. Make sure the domain name is included in the redirect target. Comment your code.


 3:16 pm on May 30, 2009 (gmt 0)

I am not using them on my site but at my localhost I made a folder thats why it is linked like this... And I am not ignoring your suggestion but it is not working thats why I am asking that is my query is right ...

If you answer me I will be very thankful to you

please answer man ..


 3:17 pm on May 30, 2009 (gmt 0)

ok man very thankful to all of you and specially g1smd and jim

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved