Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

apache - .htaccess for subfolder

I have the following structure

-www
  -subfolder

In www I have my main site's index.php.

In subfolder I have a sort of admin UI and in there I'd like to have another index.php for the admin UI.

Currently my requests from within /subfolder/index.php get redirected to www/index.php and basically the pages of my admin UI don't show up.

This is my .htaccess file:

RewriteEngine On
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ index.php?lang=$1&page=$2 [L]

Can you help me? I've tried several options in other answers but as I'm no so advanced a web developer, I couldn't get any to work.


@TerryE, Sorry if I have come off as crude.

I am using a local setup for testing. I have installed Vertrigo server, which gives me Apache server.Running on Windows7 OS. The server is installed in Program filesVertrigoServApache folder.

My public folder is www. In there I have my main site definition. . The site is accessed locally via 127.0.0.1/index.php or 127.0.0.1/

I have site localization so URLs are constructed as /$lang/$page e.g. <a href=" / <?php echo $lang; ?> / home "> HOME </a>

In index.php of main site I have the following:

$page = trim( ( isset( $_GET[ 'page' ] ) ? $_GET[ 'page' ] : 'home' ), '/' );

$lang = trim( ( isset( $_GET[ 'lang' ] ) ? $_GET[ 'lang' ] : 'en' ), '/' );
$langs = array( 'en', 'fr', 'ru' ); 

And upon this data I get to open the pages this way:

include 'html/'. $lang . '/' . $page . '.php';

All my main site's pages lie in www/html/$lang/

$_SERVER['REQUEST_URI']) gives /en/home for page HOME.

127.0.0.1/en/home WORKS

All navigation works perfectly for the main site.

However I have created an admin UI which lies in folder www/admin - one level below in www.

And in there I don't have any localization. I just have EN as language. So at the top of the index.php in admin folder I have again

$page = trim( ( isset( $_GET[ 'page' ] ) ? $_GET[ 'page' ] : 'home' ), '/' );

However, here navigation is as follows <a href=/admin/home"> HOME </a>

and upon this I get to construct the pages in the index.php in admin folder as follows:

include 'html/ . $page . '.php';

the pages lie in www/admin/html

This does not work at all. Whenever I press home link in admin UI, I get redirected to my main site (non-existing page). If I add RewriteRule ^subfolder/ - [L] in .htaccess, I get HTTP 404 NOT Found error.

127.0.0.1/admin/home DOES NOT WORK. Neither does any other navigation from within admin. Thank you for your willingness and patience to help me!

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

I assume from this that you only have a single .htaccess file in your www directory.

Think about what the rule

RewriteRule ^(.*)/(.*)$ index.php?lang=$1&page=$2 [L]

does when interpreted in a Perdir context from www: take any URI of the form someDir/somePage and replace it by index.php?lang=someDir&page=somePage, so this will intercept and rewrite any /subfolder/index.php.

This isn't well documented but Perdir processing will preferentially use the lowest .htaccess file setting RewriteEngine On on the request path. So if you add an admin-specific .htaccess file in the "subfolder" subfolder, this will preempt the www one and circumvent this problem.

Postscript comments

Veni and other posters get in a Q&A when the real issue is one of "how do I debug my .htaccess rules if I my website is hosted by a shared service?" The reason that I add the shared service qualification is that if you have root access to your LAMP config then you can turn on Rewrite logging and the logfile at a debug level of 4-6 will give you enough forensics to work out what is going on.

However, the large majority of hobby / small service users buy their services on a shared basis and here they don't have root access and the hosting provider disables such logging for performance reasons so they have a binary feedback -- the rules work or they don't. I use a shared service and my approach (described here) is to set up a VM which mirrors this configuration for as a test and integration environment -- and in this I have such root access. However, this is probably far too complicated for most users. This is really a wider topic that merits its own Q / discussion.

On specific points:

  • If your structure is /(lang|admin)/page, then why do you have this rule because it can cause havoc on perdir retries.

    RewriteRule ^$ index.php [QSA,L]
    
  • Better something like the following to force a redirect to a default language (assume the lang list is EN and IT in this example:

    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteCond $1 !^/(en|it|admin)/
    RewriteRule ^(.*) http://yourdomain/en/$1 [R=301,L]
    
  • Are you aware of the redirect restart /looping issues? (search for REDIRECT_STATUS)

I'm new to Stackoverflow, but not to sorting out this sort of s**t. I've got a couple of detailed articles on my blog on this.

Postscript comments -- yet more

OK some general help.

  • Don't us fixed IPs put an alias for 127.0.0.1 in your windowssystem32driversetchosts
  • You can turn on rewrite logging in your VertrigoServ Apache config and this gives you a detailed audit of where you have problems
  • It also helps if you have a little diagnostic stub to help you understand what is going on and this is what I came up with for index.php in the test directories:

    <?php header( "Content-type: text.plain" ); echo "main: "; var_export($_GET);
    

But you really need for each of the following cases: - to handle the URI exists (and stop rewrite loops) - the defaults for / and /admin/ - admin/* - */* - * (and the language defaults)

and this is what I came up with. I've kept it simple. We could use complex regexps to fold some of these but why bother. You may need to add more QSAs if needed.

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f                [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .  -                                  [L]

RewriteRule ^$ index.php                          [QSA,L]
RewriteRule ^admin/?$ admin/index.php             [QSA,L]

RewriteRule ^admin/(.*)$ admin/index.php?page=$1  [L]

RewriteRule ^(.*)/(.*)$ index.php?lang=$1&page=$2 [L]

RewriteRule ^(.*)$ index.php?lang=en&page=$1      [L]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...