Rewrite URLs help

Status
Not open for further replies.

sperko

Member
Messages
258
Reaction score
4
Points
18
I am new to using .htaccess so I hope you can help me

I would like to rewrite www.example.com/hello/test.html to www.example.com/hello/test/

I have many pages organized in separate directories for example:

/community/page1.html
/community/page2.html
/news/sitenews.php
/news/worldnews.php
/resources/stuff1.html
/resources/stuff2.html


and I would like those pages to appear as follows:

/community/page1/
/community/page2/
/news/sitenews/
/news/worldnews/
/resources/stuff1/
/resources/stuff2/


Thank you! :)
 

sperko

Member
Messages
258
Reaction score
4
Points
18
Ok I have made it work but I'm not sure if it's correct please can you check

I have created a directory called /testdir/ - inside this directory are 2 files called file_1.html and file_2.html

My .htaccess in /testdir/ looks like this:

RewriteEngine On
RewriteRule ^test1$ file_1.html
RewriteRule ^test2$ file_2.html


When I access /testdir/test1 - I can see the content from file_1.html
When I access /testdir/test2 - I can see the content from file_2.html

Is this the correct way to do it? Thanks :D
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
It's a right way to do that, but you're going to wind up with a few billion rewrite rules if your site gets to be any size, and that wouldn't be good. If you've got a combination of static files and scripts, the best method would probably be to first convert to a file (.html), and serve that file if it exists. If it doesn't exist, then rewrite again to a script parameter/parameter set. (Or try a different file type for giggles first before going to the nuclear option.)

There's a reasonably understandable mod_rewrite guide at Apache.org that's probably worth looking at. It sort of assumes you've got a basic handle on regular expressions, though, and that's really the hard part of URL rewriting (even if it's a simple example of RegEx use). Still, for open source documentation, it's pretty not too painful.
 

sperko

Member
Messages
258
Reaction score
4
Points
18
Thank you essellar!

I was hoping for an easier way because like you said, if I have many pages I will need to add them all to the .htaccess to rewrite them...

The guide looks very detailed so I will read through it and look for a solution!

:D
 

sperko

Member
Messages
258
Reaction score
4
Points
18
** edit **

My new .htaccess simply removes the .html and .php file extensions from the files in /testdir/ so I can access example.html by going to /testdir/example

Here is my .htaccess
Code:
RewriteEngine on

RewriteBase /testdir

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteEngine on

RewriteBase /testdir

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
 
Last edited:

sperko

Member
Messages
258
Reaction score
4
Points
18
All fixed! See my edit :)

Just a quick question is it necessary to put RewriteEngine on and RewriteBase /testdir twice in the .htaccess?
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
No, just the once will do. You only need to turn the engine on once, and you don't need to set the rewrite base again unless it changes (like with a new condition/ruleset that points to a different place after the first set of checks is done).
 
Status
Not open for further replies.
Top