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
Solved

URL rewriting

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

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

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

Suggested Solutions

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…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
This video teaches users how to migrate an existing Wordpress website to a new domain.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

840 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