Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

rewrite a folder name using .htaccess

I am wondering wether it's possible to use .htaccess to rewrite a folder name. What I mean is this.

Lets say I have a url like:

www.site.com/folder1/page.php

Now I want to rewrite the url to (for example)

www.site.com/apple/page.php

The folder1 is an existing folder on my webspace.

important: the "apple" is not a folder rather just a name!

Ok here is a step by step plan:

  1. User types www.site.com/folder1/login.php
  2. The url should rewrite and not redirect the url to www.site.com/apple/login.php

This means that apple is just a name and not a directory. All the code should just come from folder1. Acutally apple should just be an alias for folder1. I can't just rename folder1 to Apple. Therefor I would just rewrite folder1 to apple.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

mod_rewrite can only rewrite/redirect requested URIs. So you would need to request /apple/… to get it rewritten to a corresponding /folder1/….

Try this:

RewriteEngine on
RewriteRule ^apple/(.*) folder1/$1

This rule will rewrite every request that starts with the URI path /apple/… internally to /folder1/….


Edit????As you are actually looking for the other way round:

RewriteCond %{THE_REQUEST} ^GET /folder1/
RewriteRule ^folder1/(.*) /apple/$1 [L,R=301]

This rule is designed to work together with the other rule above. Requests of /folder1/… will be redirected externally to /apple/… and requests of /apple/… will then be rewritten internally back to /folder1/….


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...