mysite.com/dir1/dir2/dir3 TO mysite.com/dir1-dir2-dir3

New to mod_rewrite? This is a good place to start.

mysite.com/dir1/dir2/dir3 TO mysite.com/dir1-dir2-dir3

Postby adsforjon » Mon Aug 24, 2009 9:20 am

It may seem like a silly or nonsensical request, but hear me out.

My boss is unreasonably adamant about having all the URLs on the root level for SEO reasons. And even though I can simply create the pages with that URL, it'd be much cleaner to keep the pages organized in a directory tree.

I'm pretty new to mod_rewrite, but here's what I have so far:

Code: Select all
RewriteEngine On
RewriteRule ^/*$ - [NC]


Any ideas?
adsforjon
 
Posts: 2
Joined: Mon Aug 24, 2009 8:42 am

Clarification

Postby adsforjon » Mon Aug 24, 2009 11:43 am

Not sure if I was clear earlier, but here is an example of what I want to do:

I have the following file structure

+ Folder 1
- index.php

- Subfolder 1
- index.php

- Super Subfolder 1
- index.php

- Super Subfolder 2
- index.php

- Super Subfolder 3
- index.php

- Subfolder 2
- index.php

- Super Subfolder 1
- index.php

- Super Subfolder 2
- index.php

- Super Subfolder 3
- index.php


So the URL is currently http://www.mysite.com/folder1/subfolder1/index.php

I'd like to rewrite the URL to appear as http://www.mysite.com/folder1-subfolder2

Any ideas yet?
adsforjon
 
Posts: 2
Joined: Mon Aug 24, 2009 8:42 am

Postby richardk » Mon Aug 24, 2009 2:21 pm

How deep are you going to go? (How many dashes?)

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(?:([^-]+)-(?:([^-]+)-(?:([^-]+)-)?)?)?([^-]+)$ /$1/$2/$3/$4 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 35 guests

cron