Solved

URL rewriting

Posted on 2009-03-31
5
385 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 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.

691 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