We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

How do I use mod_rewrite to change example.com/1/2/3 to example.com/3 ??

Geesu
Geesu asked
on
Medium Priority
289 Views
Last Modified: 2010-03-04
Is this possible?

Lets say I have this URL:

http://example.com/1/2/3/

And I want to use mod_rewrite to make it look like:

http://example.com/3/

Or is this not possible?

Thanks!
Josh
Comment
Watch Question

Top Expert 2008

Commented:
If you define a rule per each string, yes. Because I can't see any pattern in your example to use a more flexible regular expression.

RewriteEngin on
# rewrit incomming request /3/ --> /1/2/3
Rewriterule ^3/$ /1/2/3/ [L]

are you planning to use that in a .htaccess file or in httpd.conf?
Top Expert 2008

Commented:
it should be
RewriteEngine on
(missing 'e')

Author

Commented:
woa thats confusing, don't think I can do this myself...

Well basically what I need the rule to be like will depend on what is clicked on...  (I have a web archiving website)...

So one link will be:

example.com/data/March-2006/20/thesitewillbehere/

which will be re-written as:

example.com/thesitewillbehere/

And the March-2006 and the 20 can change dynamically as well as the folder name for "thesitewillbehere", is this possible to do with a rule?
Top Expert 2008

Commented:
if you access example.com/data/March-2006/20/thesitewillbehere/ you can rewrite it to example.com/thesitewillbehere/ (so example.com/thesitewillbehere/ exists physically) the other way /data/March-2006/20/thesitewillbehere/ exists but you're calling /thesitewillbehere/ will not work. You must use a RewriteMap or a rule for each string in that case.

RewriteEngine on
# /data/March-2006/20/thesitewillbehere/ --> /thesitewillbehere/
RewriteRule ^data/[^/]+/[^/]+/([^/]+)/$ /$1/ [L]

Author

Commented:
Is it possible to have thesitewillbehere be the same???

In theory that directory is supposed to be the same each month this tool runs...  SO I guess I should try to make them different?

For example maybe doing like example.com/data/March-20-2006-thesitewillbehere which I can then re-write as example.com/March-20-2006-thesitewillbehere

These directories are created dynamically each month, so is it possible to auto-add a new rule everytime a directory is created?  Or is there a way to make this not necessary?
Top Expert 2008
Commented:
request of                                                      --> internally to
example.com/March-20-2006-thesitewillbehere --> example.com/data/March-20-2006-thesitewillbehere or
example.com/March-20-2006-thesitewillbehere --> example.com/data/March-2006/20/thesitewillbehere/

would be possible.

example.com/March-20-2006-thesitewillbehere --> example.com/data/March-2006/20/thesitewillbehere/
RewriteRule ^([^-]+)-([0-9]+)-([0-9]+)-([^/]+)$ /data/$1-$3/$2/$4/ [L]

example.com/March-20-2006-thesitewillbehere --> example.com/data/March-20-2006-thesitewillbehere
RewriteRule ^([^-]+-[0-9]+-[0-9]+-[^/]+)$ /data/$1 [L]

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Wow dude thanks, I need to learn regular expressions it seems :)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.