Forum Moderators: phranque

Message Too Old, No Replies

Rewrite html to file path

How to rewrite from html file to filepaht

         

kpuchino

9:15 pm on Feb 6, 2011 (gmt 0)

10+ Year Member



Hi all,

I've spent the last three days reading through loads of forums in an attempt to make an easy rewrite, that I am sure that you could help me. Thanks

I have one domain with html files, and I want to rewrite it to some folder inside de domain, I mean:

www.example.com/page1.html
www.example.com/page2.html
...

and I want to make work:

www.example.com/page1/
www.example.com/page2/
...

I also want that the rewrite will be SEO friendly as I have some good results with the original html files and the new linkbuilding strategy will be focused in the path file.

Thanks!

g1smd

9:22 pm on Feb 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The
RewriteRule
pattern should match the path part of the URL that was requested.

The
RewriteRule
target specifies the local internal path and filename where the content actually resides inside the server.

Finish off with the
[L]
flag.

kpuchino

9:24 pm on Feb 6, 2011 (gmt 0)

10+ Year Member



Could you detail me please, so I am not really skill on developer, and my team is on holidays :S

If you could just type me how it should be would really great! Thanks!

kpuchino

10:06 pm on Feb 6, 2011 (gmt 0)

10+ Year Member



This is what I have in my .htaccess

RewriteEngine On
RewriteRule ^page1/$ /page1.html
RewriteRule ^page2/$ /page2.html

The problem is that when I type in my browser:

www.example.com/page1/

This works, but none image is display, it lost the source of all the resources.

g1smd

10:43 pm on Feb 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Add the [L] flag to every RewriteRule.

The problem with images is that as a result of the rewrite, a link to
src="image.png"
results in the browser asking for
example.com/page1/image.png
.

In this case, you need to link to
src="/image.png"
or to
src="/images/image.png"
with a leading slash and the full path to the image file.

The problem is not the rewrite. The problem is the links on your pages pointing to the images.

kpuchino

10:51 pm on Feb 6, 2011 (gmt 0)

10+ Year Member



Ok, so I have to change all the sources of all the files with the / before?

I assume there is no way with this.

On the other hand, there is other way to rewrite automatically ALL the pages, instead of adding all lines one by one?

Thanks!

g1smd

11:25 pm on Feb 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, there is a simple way to rewrite all requests to the correct file, but only if there is a simple "relationship" between what is in the URL request and what the filename is on the server.

One caution. You use URLs with a trailing slash. That is reserved for folders and for the index page in a folder. If you desire extensionless URLs, remove the trailing slash. This then makes for a far simpler URL pattern-matching exercise.

This example code rewrites requests for
/<something>
to
/<something>.html
where
<something>
does not contain a period:
RewriteRule ^([^.]+)$ /$1.html [L]


Once the rewrite is working, you should redirect external requests for .html URLs to the new URL. This additional external 301 redirect needs a preceding RewriteCond looking at THE_REQUEST if it is to not introduce an infinite loop.