Subfolders and mod-rewrite - apache

I need to alter this current re-write rule to accommodate for an admin folder. Here is my current mod-rewrite code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+?)/([a-z]+?)/(.*)$ index.php?model=$1&view=$2&params=$3 [L,NS]
I have this folder structure:
ROOT: http://www.domain.com/
ADMIN: http://www.domain.com/admin/
If the .htaccess file is in the "admin" folder it works correctly. I get:
URL: http://domain.com/admin/faq/edit/13/asc
(NOTE: http://domain.com/admin/.htaccess)
Array
(
[model] => faq
[view] => edit
[params] => 13/asc
)
But, when its in the root folder I get:
URL: http://domain.com/admin/faq/edit/13/asc
(NOTE: http://domain.com/.htaccess)
Array
(
[model] => admin
[view] => faq
[params] => edit/13/asc
)
I want the mod-rewrite to recognize that admin folder and is an actual directory and use a separate re-write rule.
Thanks in advance.

Add
RewriteCond ^admin/
RewriteRule (your Rule) [L]
before the other condition. That should do the trick.
Another way would be to include the condition in the rule directly via
RewriteRule ^admin/(rest_of_regex)$ (regex_stuff) [L]

Add this to your root .htaccess:
RewriteCond ^admin/
RewriteRule (whatever rule you need for the admin folder) [L]
Add this right after the RewriteEngine On. The key point here is [L] which tells mod-rewrite to stop executing the rest of the rules if this one matches. If you need more than one rule for the admin folder, just add more RewriteRule statements, but remember to only add the [L] to the last one.
That should do it.

Related

htaccess rewrite root url to subfolder with accessing other folder?

I have following folder structure in apache server.
Public_html
-->admin
--->admin_login.php
-->website
--->index.php
since the index.php inside the website folder,
i have given following code in .htaccess
RewriteCond %{REQUEST_URI} !^/website/
RewriteRule ^(.*)$ /website/$1 [L,NC]
so that the when the user enter root url , it will appear "www.myurl.com" instead of "www.myurl.com/website/"
but the issue is, i could not be able to access admin_login.php.
is there anyway to modify .htaccess, to come website/index.php in main url and able to access admin_login.php(both)?
Thanks in Advance
You need to add an exception to existing rule like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(website|admin)/ [NC]
RewriteRule .* website/$0 [L]
Negative condition %{REQUEST_URI} !^/(website|admin)/ will match every URI except URIs that start with /website/ or /admin/. This will allow you to directly open www.myurl.com/admin/admin_login.php.
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php/?$ website/index.php [QSA,NC,L]

exclude rewrite rule on folder for mvc

I have the following in my .htaccess, it rewrites all to a central file,
I want to bypass it for a certain folder (& all its subfolders), the folder I want to bypass is named "admin" , how should I modify this rewrite to stop rewriting for admin folder?
I checked this question but wasn't able to get the needed results:
.htaccess mod_rewrite - how to exclude directory from rewrite rule
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
Perhaps the easiest way is to put an .htaccess in the admin folder with nothing but: RewriteEngine Off.
To exclude the admin folder from Your rule ,you can use :
RewriteCond %{REQUEST_URI} !^/admin.*
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]

.htaccess excluding directory issue

I have a website consisting of a single index.html file. I have several menus, and need to go 3 levels deep, so I want a php-like structure as such: index.html?main=$1&secondary=$2&tertiary=&3. This has to be reduced to www.example.com/$1/$2/$3. Overall pretty simple I would think, using following rules for in .htaccess:
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)?$ index.html?main=$1&secondary=$2&tertiary=$3
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)?$ index.html?main=$1&secondary=$2
RewriteRule ^([A-Za-z0-9-]+)?$ index.html?main=$1
Now, I also have several folders in my root folder that shouldn't be affected, otherwise my include's wont work. Looking at this answer I've already tried exluding these folders using RewriteRule ^(bower_components|photos)($|/) - [L] before the other rules, but it didn't work. I've also tried this answer, making a .htaccess with contens RewriteEngine Off and putting it in my folders, also without success. So obviously I'm doing something wrong somewhere. To show my folder layout, here's a quick snapshot of it:
Here's my current .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)?$ index.html?main=$1&secondary=$2&tertiary=$3
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)?$ index.html?main=$1&secondary=$2
RewriteRule ^([A-Za-z0-9-]+)?$ index.html?main=$1
Now, if I go to http://localhost/myProject/activities, so 1 level deep as the index.html file is located in myProject, it does work and all includes are included correctly. However, when going to http://localhost/myProject/activities/test, I get to the basic index.html page, but my includes point to http://localhost/myProject/activities/bower_components/platform/platform.js, so the activities is too much.
Keep your rules like this:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /myProject/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.html?main=$1&secondary=$2&tertiary=$3 [L,QSA]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.html?main=$1&secondary=$2 [L,QSA]
RewriteRule ^([A-Za-z0-9-]+)/?$ index.html?main=$1 [L,QSA]
Then for css/js/image inclusion just use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
You can also try adding this in your page's HTML header: <base href="/myProject/" /> so that every relative URL is resolved from that URL and not the current URL.

.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 files\VertrigoServ\Apache 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. HOME
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 HOME
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!
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 windows\system32\drivers\etc\hosts
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]
RewriteEngine On
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^subfolder/(.*)$ subfolder/index.php?page=$2 [L]
RewriteRule ^(.*)/(.*)$ index.php?lang=$1&page=$2 [L]
You could prevent requests to you subfolder from being rewritten with the change below
RewriteEngine On
RewriteRule ^$ index.php [QSA,L]
#prevent requests to your subfolder from being rewritten
RewriteCond %{REQUEST_URI} !^/subfolder/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ index.php?lang=$1&page=$2 [L]

mod_rewrite prefix as a folder

I have this structure in my home directory:
/index.php
/.htaccess
/folderx/
/folderx/subfolder/
/folderx/subfolder/file.php
What I need, is to mod_rewite all requests with the prefix "folderx_" to the real folderx directory. But additionally, I need to force redirection - the user should't be available to request /folderx/subfolder/subsubfolder/file.php directly. It should be redirected to the mod_rewrite url: /folderx_subfolder/subsubfolder/file.php
/folderx_subfolder/ => /folderx/subfolder/
/folderx_subfolder/file.php =>
/folderx/subfolder/file.php
/folderx_subfolder/subsubfolder/ =>
/folderx/subfolder/subsubfolder/
/folderx_subfolder/subsubfolder/file.php
=> /folderx/subfolder/subsubfolder/file.php
I've tried the code below, but the redirect is not working properly. When requesting the subfolder, it redirects to the prefix, but I don't know how to prevent looping( Please, help!
/folderx/subfolder/subsubfolder/file.php
RewriteCond %{REQUEST_URI} ^/folderx/(.*)$
RedirectMatch ^/folderx/(.*)$ /folderx_$1
RewriteRule ^folderx_([a-z]{3,15})(/?)+$ /folderx/$1/ [NC]
RewriteRule ^folderx_([a-z]{3,15})/(.*)$ /folderx/$1/$2 [NC]
RewriteRule ^folderx/(.+)$ http://example.com/folder_$1 [nc,last,redirect]
RewriteRule ^folderx_(.+)$ /folderx/$1 [nc]