Hi Guys,
Could someone please show me how to do rewrites in htaccess, I don't know much about how to do them :p
What I'm trying to acheive is this.
/file/ goes to file.php
/file/file2/ goes to /file/file2.php (file2.php inside a physical directory)
It would also be nice if when you access the file directly it goes to the directory it rewrites to. e.g. goto file.php redirects you to /file
Is this possible? The main thing I am trying to achieve is like MyBBoard's site structure. e.g.
http://mybboard.net/about/mybb.
Any help is appreciated
RewriteEngine On
RewriteRule ^new-file-name(/)?$ /path/to/file.php
Then,
www.yoursite.com/path/to/file.php becomes
www.yoursite.com/new-file-name/
The new URL will work with or without the trailing slash.
If you have, say a page with a bunch of subpages like so:
- about.php?page=us
- about.php?page=license
- about.php?page=mission
You can do this to shorten it up:
RewriteEngine On
RewriteRule ^about:([^/]*)$ /about.php?page=$1 [L]
Your page, about.php?page=mission would become about:mission, same for all of them.
Hope this helps!
It certainly does help Xiofire thanks. I've always wondered how URLs like about:link work
I'll give it a try
I wasn't planning to make pages inside one file but I think that might work :p
Any other suggestions..?
Ok no i doesn't seem to work with my current system.
Currently I have:
RewriteEngine On
Rewritecond %{REQUEST_URI} !^.*(./img).*$ [NC]
RewriteRule ^([a-zA-Z0-9]+)(/.*)?$ /site/$1\.php$2
and
RewriteRule ^download/([^/]*)$ /download.php?page=$1 [L]
doesn't work with this
Try...
RewriteRule ^download/([^/]+)?$ /download.php?page=$1 [L,NC]
(2009-12-05, 09:18 PM)Polarbear541 Wrote: [ -> ]Ok no i doesn't seem to work with my current system.
Currently I have:
RewriteEngine On
Rewritecond %{REQUEST_URI} !^.*(./img).*$ [NC]
RewriteRule ^([a-zA-Z0-9]+)(/.*)?$ /site/$1\.php$2
and
RewriteRule ^download/([^/]*)$ /download.php?page=$1 [L]
doesn't work with this
Haven't tested any of these rules, but since you asked, here some guesswork:
Your regexp all look strange... ^.*(./img).*$ why not ^.+/img.*$
(/.*)? why is there a ?
$1\.php$2? You need to escape . only in the regexp if you want to match ., there is no point in adding \ in the rewrite target file unless the file is actually named this way
The a-z/ rule seems to collide with the download/ rule because a-z/ also matches download/? You should avoid rewrite rule conflicts at any cost.
Also if either of those directories also exist physically it will stop working because a physical directory overrides the rewrite rules in your parent .htaccess. You need to add another .htaccess there telling it to inherit the parents rules... that's why it's generally a bad idea to mix real directories with directory rewrites, it makes things so much more complicated.
Ok I found these rewrites off some website. So you're saying it's probably best just to use direct directory rewrites instead of wildcards?
All I'm basically trying to do is make it like MyBBoard's directory structure. e.g. mybboard.net/about/mybb
(2009-12-04, 01:36 PM)Polarbear541 Wrote: [ -> ]Hi Guys,
Could someone please show me how to do rewrites in htaccess, I don't know much about how to do them :p
What I'm trying to acheive is this.
/file/ goes to file.php
/file/file2/ goes to /file/file2.php (file2.php inside a physical directory)
It would also be nice if when you access the file directly it goes to the directory it rewrites to. e.g. goto file.php redirects you to /file
Is this possible? The main thing I am trying to achieve is like MyBBoard's site structure. e.g. http://mybboard.net/about/mybb.
Any help is appreciated
If you want to to change the .php to / then you can do this with a simple rewrite. Instead of using directories you can use a different rewrite to get the multiple /page/page/ effect.
I posted this on DP today, I use them both. I shall quote my post here:
BlueEew;13076931 Wrote:I am currently using two methods for one website to create two different URLs.
Here are the following:
# Force search engines to use http://www.domain.com
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteRule ^(.*) http://www.domain.com/$1 [R=301,L]
# Specify search friendly URLs
RewriteRule ^category/subject/page-name/?$ /category-subject-page-name.php [L]
The above to create URLs with more then one / in them, then the following:
RewriteRule ^page-name/?$ /page-name.php
The above to take the .php anf make it a pretty URL.
Remember to always add "/?$" without the quote to the end of all pretty URLs. This will make the page work if someone enters a / or not on the end of a web-page URL for example:
http://www.domain.com/page/
http://www.domain.com/page
Both will work as long as it as "/?$" on the rewrite.
Also the part I have bolded will also make your domain redirect to www. even if you don't add the www. when entering URL. Take the www. off this part to make it just http://.
Thanks I'll try this later..