Solved

URL rewriting

Posted on 2009-03-31
5
384 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
ID: 24030807
> 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
ID: 24030882
> 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
ID: 24030949
> 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
ID: 24031247
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
ID: 24031607
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
This video teaches users how to migrate an existing Wordpress website to a new domain.
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.

749 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