简体   繁体   中英

How to configure htaccess for beta(subdirectory) keeping the root htacess as it is

I have live website in the root folder, now I have created a sub-directory to keep the development changes and test them on server, How Can I configure htaccess for subdirectory, I want to keep the live website running.

Currently my htaccess is as follows, similar to root htaccess

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]


RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f


RewriteRule ^(member-([0-9]+)-([a-zA-Z0-9\-]+))/?$ team.php?id=$2&%{QUERY_STRING} [NC,L]

Please help me

Edit : I want to access beta site by just adding /beta at the end of the url. ex https://www.example.com/beta

Multiple .htaccess files

You should probably be OK by simply creating the same .htaccess in your sub-directory and just removing the RewriteBase command - it really isn't needed in most cases, and except when rewriting paths to files on the document root, configuring it correctly can be quite confusing.

Here is a sample setup:

+- DOCROOT/index.php :

   <?php echo $_SERVER['PATH_INFO'];

+- DOCROOT/.htaccess :

   RewriteEngine On
   RewriteBase /
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*) index.php/$1

+- DOCROOT/subdir/index.php :

   <?php echo $_SERVER['PATH_INFO'];

+- DOCROOT/.htaccess :

   RewriteEngine On
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*) index.php/$1

With that setup, you can call http://server/some-path and see /some-path echoed back, or call http://server/subdir/other-path and see /other-path echoed back.

Single .htaccess

You can also add the rewrite rule for the sub-directory in your main .htaccess file that is in the document root - just dup the last rewrite chain (the only one that is interesting) and add a RewriteCond to the first one that limits only to the sub directory.

Taking your example above, it might look like this:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteBase /

RewriteCond %{REQUEST_URI} subdirectory/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^subdirectory/(member-([0-9]+)-([a-zA-Z0-9\-]+))/?$ subdirectory/team.php?id=$2&%{QUERY_STRING} [NC,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(member-([0-9]+)-([a-zA-Z0-9\-]+))/?$ team.php?id=$2&%{QUERY_STRING} [NC,L]

(I've also took the libery to remove the redundant RewriteEngine On command). If a request comes to the sub directory, it will be caught by the first rule, rewritten using the subdirectory context and the [L] flag will cause mod_rewrite stop processing. Otherwise it'll do the main routing. This setup is a bit too complex for my taste and I'd probably go with the additional .htaccess file.

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