简体   繁体   中英

How redirect to subfolders using .htaccess file?

I'm new in php and trying to build my website. I have created folder structure on my server listed below.

  1. development (folder)
  2. admin (folder)
  3. .htaccess ( copied from internet)
  4. index.html (file with coming soon banner)

I want to set my .htaccess file to load index.html file by default when users open (abcd.com). And subfolders would access with hitting the url like abcd.com/admin or abcd.com/developement. But I did some stupid changes in .htaccess file so it redirect the users to my admin login page which i don't want them to see. ;(

Thanks in Advance.

RewriteEngine on    
RewriteCond %{HTTP_HOST} ^(www.)?abcd.com$
RewriteCond %{REQUEST_URI} !^/admin/web/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /admin/web/$1  
RewriteCond %{HTTP_HOST} ^(www.)?abcd.com$
RewriteRule ^(/)?$ /admin/web/index.php [L]

It is a bit unclear why you actually ask this question...

As a web developer you surely should be able to find your way around the rewriting options the apache http server offers by looking into the documentation. But even if not: as @AmanjotKaur pointed out the default behavior of that http server is to serve the index.html file if present. If that is not the case then you should take a look at the DirectoryIndex directive offered by mod_dir ... Also it should be easy to simply roll back to an older version of that dynamic configuration file you used (".htaccess"), if you are using a revision control system for your development as you certainly should. Or by using a simple backup which you hopefully have ...

A general hint here is to simplify the situation: instead of trying to implement rules to direct incoming requests simply separate the contents. Your "production site" (which I understand is not usable yet) should not host any content, but just that placeholder you already have. Development should not be done on the same site! Use a separate http server for that or at least a separate host name inside the same http server. That is much easier and offers much more reliable protection.

Anyway, there are situations where one indeed wants to redirect all incoming requests to some specific document or router script, with a few explicit exceptions. This might be what you are looking for here. Since your question is a bit vague in that (your question mentions the path /admin , but your rules implement the path /admin/web/ ...) we can only give you a starting point. You will have to make your way from there. For which you undoubtedly need to take a look into the documentation of the apache http server's mod_rewrite which you want to use on that dynamic configuration file.

Here is something whici I assume implements something along the lines of what you are looking for. You certainly need to adapt it to your needs, though. You'd need to revise your question and be much more precise in what you actually want if you need further help with that. There is an edit button below your question. Use it ...

RewriteEngine on

RewriteCond %{REQUEST_URI} !^/index\.html$
RewriteCond %{REQUEST_URI} !^/admin/
RewriteCond %{REQUEST_URI} !^/development/
RewriteRule ^ /index.html [R=301]

RewriteCond %{REQUEST_URI} !^/admin/web/
RewriteRule ^/?admin/(.*)$ /admin/web/$1 [END]

RewriteCond %{REQUEST_URI} !^/development/web/
RewriteRule ^/?development/(.*)$ /development/web/$1 [END]

It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...

In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.

This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.

And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM