Solved

URL rewriting

Posted on 2009-03-31
5
378 Views
Last Modified: 2012-06-27
I've pretty much hacked together my .htaccess simply because of the sheer complexity of mod rewrite. In any event, what I've got it doing now is changing

domain.com/index.php
domain.com/about.php
etc...

to

domain.com/index
domain.com/about
etc...

and also, changing

domain.com/index.php?n=1

to

domain.com/news/1

(I'd still rather it pull domain.com/news/news-title-from-database but I'm clueless on that one so a simple id number will have to suffice)

Stupid as this sounds, I just want to make sure what I have is optimal for what I'm trying to achieve, as pointed out above.

Thanks in advance for checking this out.
DirectoryIndex index.php

Options +MultiViews

Options +FollowSymlinks

RewriteEngine on

RewriteRule news/([^\.]*) index.php?n=$1 [NC,L]

RewriteRule home/(.*) index/ [NC,L]

Open in new window

0
Comment
Question by:kingsburymedia
  • 3
  • 2
5 Comments
 
LVL 27

Expert Comment

by:caterham_www
Comment Utility
> RewriteRule news/([^\.]*)

matches /other/foo/news/1/foobar as well. I'd restrict it to /news/id

> RewriteRule home/(.*) index/ [NC,L]

If /home should become /index/, I'd use a 301 redirect. Decide whether to use a trailing slash or not, but I wouldn't mix it (your examples above are without a trailing slash).
DirectoryIndex index.php

Options +MultiViews

Options +FollowSymlinks

RewriteEngine on

RewriteRule ^news/([0-9]+)$ index.php?n=$1 [L]

RewriteRule ^home /index [R=301,NC,L]

Open in new window

0
 
LVL 5

Author Comment

by:kingsburymedia
Comment Utility
> RewriteRule ^news/([0-9]+)$ index.php?n=$1 [L]

The above could be problematic as I call ?n=all to show the full archive list and if I'm not mistaken, that looks like it is purely searching for a number to pass. I guess I could just change "all" to "0" or something like that...

> RewriteRule ^home /index [R=301,NC,L]

The above will redirect home/ to index/? I was totally confused about trailing slashes and it's been giving me some grief.
0
 
LVL 27

Accepted Solution

by:
caterham_www earned 500 total points
Comment Utility
> I guess I could just change "all" to "0" or something like that...

Yes, or implement it with a logical "or" like

RewriteRule ^news/([0-9]+|all)$ index.php?n=$1 [L]


> The above will redirect home/ to index/? I was totally confused about trailing slashes and it's been giving me some grief.

It'll redirect /home, /home/, /home/foo to /index (in this case w/o a trailing slash. If you want one, just add one).
0
 
LVL 5

Author Comment

by:kingsburymedia
Comment Utility
I've got it almost perfect for my needs and more importantly, I'm beginning to understand it a bit better. However,

When I follow a link like domain.com/home/, it ends up taking me to domain.com/index/ which is backwards from what I was looking for. Any idea why that would be or how to correct it?
0
 
LVL 5

Author Comment

by:kingsburymedia
Comment Utility
Well here's what I ended up with and it's working well enough for me (for now at least).

Thanks for the help caterham, much appreciated!
DirectoryIndex index.php

Options +MultiViews

Options +FollowSymlinks

RewriteBase /v2

RewriteEngine on

RewriteRule ^news/([0-9]+)$ index.php?n=$1 [L]

RewriteRule ^news/$ index.php?n=0 [L]

RewriteRule ^home/ index.php [NC,L]

Open in new window

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now