?
Solved

URL rewriting

Posted on 2009-03-31
5
Medium Priority
?
388 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
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…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
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.

765 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