开发者

How do I do this with apache rewrites? (alter url if it doesn't contain a 1 of 2 strings)

I've installed a .htaccess file containing rewrite rules (as suggested by CodeIgniter) seen below.

How can I add to it so that any requests for urls that don't begin with members/ or admin/ get public/ put at the beginning?

i.e. if someone requests contact/, it should actually go for public/contact and so on unless they request members/something or admin/something.

.htaccess at present:

RewriteBase /

# Removes access to the system folder by users.
RewriteCond %{REQUEST_URI} ^_system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

# prevent user access to the application folder
RewriteCond %{REQUEST_URI} ^myapp.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

# This snippet re-routes everything through index.php, unless
# it's being sent to resources, or searching f开发者_如何学Pythonor robots.text
# Add any OR's in here if you need other directly accessable Files/folders 
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteRule ^(.*)$ index.php/$1  


.htaccess - edited - this is how it looks now:

RewriteCond %{REQUEST_URI} ^_system.*
RewriteRule ^(.*)$ /index.php?/$1

RewriteCond %{REQUEST_URI} ^myapp.*
RewriteRule ^(.*)$ /index.php?/$1 

RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteRule ^(.*)$ index.php/$1      

RewriteCond %{REQUEST_URI} !^/(members/|admin/|public/|index\.php) [NC]
RewriteRule ^(.*)$ /public/$1 [L]    

It's returning a 404 for all requests at the moment. I'm sure I've got something wrong but don't know what :)

File structure is:

site_root/.htaccess site_root/index.php site_root/myapp/


You can prepend the regex with ! to mean "must not match". So you can achieve what you want like this:

RewriteCond %{REQUEST_URI} !^(public|members|admin)/ [NC]
RewriteRule ^(.*)$ /public/$1 [L]


Note: I'm adding a new answer because it is mostly unrelated to my old answer and is based on edits to your post. Leaving my old answer up for reference.

There are a number of problems here, but there is one big issue which needs resolving before it even makes sense to address the small problems. It appears that you are using a framework that works by routing everything (with a few exceptions) through index.php (which surely has its own internal router to the application controllers in myapp/). But you also say you want to route everything (with exceptions) through public/.

These are mutually exclusive possibilities -- you can't route all traffic through two places simultaneously. So I can go into fixing the above rules and show the code, but only after I know what the rules are supposed to be doing. Given what you said you want in your intro text, combined with what is already in your .htaccess, it's impossible to figure out what the combined rules are even supposed to do. I can edit this post with code details only after you answer some questions:

  • What framework are you using?
  • What is public/ supposed to be? Is it a directory (if so, what should it hold)? Or is something that is supposed to be routed to and handled by the application?
  • Do your current admin and members sections work by going to admin/ and members/ subdirectories? Or are these routed to and handled by the application?
  • Do you have any other important details about how the app is structured?
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜