This is the architecture of my website :
/
app/
index.php
...
libs/
lib1/
file.php
lib2/
...
I need to access index.php by this url : domain.com/index.php
I tried this :
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !(.*)app
RewriteRule ^(.*)$ app/$1 [L]
It works, but inside my index.php, I call for example :
include('../libs/lib1/file.php');
It's doesn't work because this path refer to root now...
And I can't access to domain.com/libs anymore, because it's looking for domain.com/app/libs.
How can I do ?
The include() shouldn't care what the path the browser sees. That should be based on the local filesystem on the server. But your rules are affecting direct access to the libs, so try adding a few more conditions:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^app/
RewriteRule ^(.+)$ app/$1 [L]
RewriteRule ^$ app/index.php [L]
This makes it so requests for existing files or content won't get routed through the app folder.
Related
I have this URL:
http://www.register.abc.com/myapp/administrator/registration
Which is :
http://www.register.abc.com is my domain
myapp is my aplication folder in server
administrator/registration is my controller and function
What I'd like to do is to remove this part /myapp/administrator/registration from my URL. In default, when user only enter the http://www.register.abc.com/ it will be redirected to my home page. I'd like to set it so the user directly enter registration page not the home page. I have little to no knwledge about .htaccess. Any help is greatly appreciated.
Note: I am using wiredesignz codeigniter modular extensions hmvc
Edit:
my current .htaccess is like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /myapp
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
In your site root .htaccess (a level above myapp/ you may try this .htaccess code:
RewriteEngine On
RewriteRule ^$ myapp/administrator/registration [L,R=302]
If you want an internal rewrite (without changing URL) then try:
RewriteRule ^$ myapp/administrator/registration [L]
I have a webproject saved in a "base" folder of the domain "example.com" which contains a main.php and several subfolders. The second part of the code below redirects all requests (except main.php itself and page404.php) to this main.php and hands over the originally requested URL in the variable "path".
In addition there is a first part which redirects all requests of pages of the baseurl to a subfolder "folder1". So in the end the request of
www.example.com/somepage.php
will lead to
example.com/main.php?path=www.example.com/folder1/somepage.php
(Requests to other subfolders shall only be redirected to the main.php. So www.example.com/somefolder/somepage.php will lead to example.com/main.php?path=www.example.com/somefolder/somepage.php - without adding "folder1".)
The code below actually does what I want:
ErrorDocument 404 /page404.php
Options -Indexes
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine on
#first part: redirect to folder1/
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/*
RewriteCond %{REQUEST_URI} !^page404*
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ folder1/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ index.php
#second part: redirect to main.php
RewriteCond %{REQUEST_FILENAME} (.*)\.php [NC]
RewriteCond %{REQUEST_URI} !main\.php
RewriteCond %{REQUEST_URI} !page404\.php
RewriteRule ^([^?]*)$ /main.php?path=$1 [NC,L,QSA]
</IfModule>
But I have two questions:
Side question: I have the feeling that actually redirecting to "folder1" is way to complicated (even though it works) when I only want to add the "folder1" to the path variable in case a file of the base folder is called. Can you show me a better way to archieve this?
Main question: I have the same project on a localhost where the main folder is named "folder1" which contains the folder "folder1". So there the request of htttp://localhost/folder1/somepage.php shall lead to htttp://localhost/folder1/main.php?path=htttp://localhost/folder1/folder1/somepage.php (I replaced "http" by "htttp" to avoid automatic link recognition of SO) - How do I have to change the code above to have it working in the "localhost/folder1" scenario?
This stuff can be simplified:
ErrorDocument 404 /page404.php
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
# ignore main.php, page404.php OR any files/directory for any rules below
RewriteCond %{REQUEST_URI} ^/(main|page404)\.php
RewriteRule ^ - [L]
#first part: /folder1/path
RewriteRule ^([^/]+?\.php)$ main.php?path=folder1/$1 [L,QSA]
#second part: rewrite to main.php
RewriteRule ^(.+?\.php)$ main.php?path=$1 [L,QSA]
We have a server, and have several folders under the /var/www/ folder.
We have pointed our domain name to the IP of the server, and by default we expect to load one folder as the website (since its not possible to point a folder with DNS).
I have written .htaccess in such a way that when you enter the IP or the domain name, the request redirects to the website folder.
However, whenever we enter the IP or the domain name, the name of the folder is getting added to the URL.
Here is the present .htaccess:
Options +FollowSymlinks -Multiviews
#DirectoryIndex folder/
RewriteEngine on
ReWriteBase /
RewriteRule ^$ /folder [L]
RewriteRule ^$ folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^folder/)^(.*)$ /folder/$1 [L,NC]
where the folder is the folder's website
so,
www.domain.com
becomes
www.domain.com/folder/
Is there a way to rewrite the URL to remove the folder name?
Thanks in advance :)
EDIT : Added .htaccess code
Have your rule like this in DocumentRoot/.htacess:
DirectorySlash On
Options +FollowSymlinks -MultiViews
RewriteEngine on
ReWriteBase /
# redirect /folder/abc123 to /abc123
RewriteCond %{THE_REQUEST} \s/+folder/(\S*) [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# skip rewrites for real files/directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule !^(index\.)?$ - [L]
# route everything internally to /folder
RewriteRule ^((?!folder/).*)$ folder/$1 [L,NC]
It sounds like you made an external redirect instead of an internal rewrite. An external redirect is denoted by the [R] flag (with optional parameter) and causes Apache to send a 301 or 302 header back to the client with a different url that client should request. This causes the client to show the new url in the address bar.
What you want is an internal rewrite. When you request the url, the url is internally rewritten to the place where the resource is actually located. You do this by omitting the [R] flag, and not using a domain name in the rewritten part. It typically looks something like this:
RewriteCond %{REQUEST_URI} !^/folder
RewriteRule ^(.*)$ /folder/$1 [L]
OK, I'm pants at rewrite rules in .htaccess files!
My desired scenario is (using the URL http://doma.in/ as an example):
First check to see if an index.html file exists in the /public sub-dir; if it does, serve it
If it did not; serve (rewrite to) index.php
To expand on my example, say we requested the URL http://doma.in/js/foobar.js:
First check to see if an foobar.js file exists in the /public/js sub-dir; if it does, serve it
If it did not; serve (rewrite to) index.php?controller=js%2Ffoobar.js
That would cover static files but I also need URLs like http://doma.in/foo:
First check to see if an foo file exists in the /public sub-dir; if it does, serve it
If it did not; serve (rewrite to) index.php?controller=foo
And a URL http://doma.in/foo/bar:
We can assume the file foo/bar does not exists in the /public sub-dir as files can't be named like that.
So serve (rewrite to) index.php?controller=foo&action=bar
I'm sure if this complicated (for me) bit is covered then I can work query-strings into the occasion too; so http://doma.in/foo/bar/foo/bar would serve index.php?controller=foo&action=bar&querystring=foo%2Fbar.
I'd also like to make sure that a trailing slash is handled the same as if a trailing slash was omitted, for example: http://doma.in/foo/bar/foo/bar and http://doma.in/foo/bar/foo/bar/
I'll handle 404s from within my app as if the file did not exist, it would redirect to index.php which does exist - I'm happy with this unless you've a better solution :)
I really hope all this makes sense as I've been looking to find a solution to this scenario all day now and this is all I have:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
#RewriteBase /prompt-v3/
#RewriteCond %{REQUEST_URI} ^/prompt-v3/(.*)$
RewriteCond $1 !^public
RewriteRule ^(.*)$ public/$1 [R]
The commented-out lines deal with a sub-dir when on a remote host. So far I can redirect to the /public sub-dir if the file exists there and that's about it.
Thank you everyone for your help!
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
#RewriteBase /sub-dir/
#RewriteCond %{REQUEST_URI} ^/sub-dir/(.*)$
RewriteRule ^index.html$ $1 [L,R=301]
RewriteCond $1 !^public
RewriteCond $1 !^lib
RewriteRule ^(.*)$ public/$1 [L]
RewriteCond $1 ^public/$
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteRule ^(.*)$ lib/bootstrap.php [L]
RewriteCond $1 !^public/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 ^public/(.*)$
RewriteRule ^(.*)$ lib/bootstrap.php?path=%1 [L]
This will look for an index.html file in the public directory and if it does not exist rewrite the URL to lib/bootstrap.php. it in fact checks for any request as a static file in the public directory first and deals with canonicalisation too.
Put this .htaccess in your document root
<ifModule mod_rewrite.c>
RewriteEngine on
# For /js/foobar.js (/js/*)
RewriteRule ^js/(.*)$ /public/js/$1 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^public/(.*)$ index.php?controller=$1 [NC,L]
# For foo/bar (/*/*?*)
RewriteRule ^(.*)/(.*)$ /public/$1/$2 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^public/(.*)/(.*)$ index.php?controller=$1&action=$2&querystring=%{QUERY_STRING} [NC,L]
</IfModule>
I've got a problem trying to install Social Engine. Upload all files, set directory permissions, but when I enter my domain, I get a:
Not Found
The requested URL /install/install was not found on this server.
It's something related to the .htaccess files in root and install directory, 'cause removing them I do begin the installation process BUT with long URLs, this way:
/install/index.php/install
And that's something that would end up with all long URLS, instead of short ones that google use to index, for example. My ISP has safe_mode OFF, and mod_rewrite ON, so I'm just kinda lost...
.htaccess :
# $Id: .htaccess 7539 2010-10-04 04:41:38Z john $
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
# Get rid of index.php
RewriteCond %{REQUEST_URI} /index\.php
RewriteRule (.*) index.php?rewrite=2 [L,QSA]
# Rewrite all directory-looking urls
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*) index.php?rewrite=1 [L,QSA]
# Try to route missing files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} public\/ [OR]
RewriteCond %{REQUEST_FILENAME} \.(jpg|gif|png|ico|flv|htm|html|php|css|js)$
RewriteRule . - [L]
# If the file doesn't exist, rewrite to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rewrite=1 [L,QSA]
</IfModule>
# sends requests /index.php/path/to/module/ to "index.php"
# AcceptPathInfo On
# #todo This may not be effective in some cases
FileETag Size
Had the same issue running SocialEngine on Vagrant (using https://box.scotch.io/) and fixed it by clearing the .htaccess file under /install/.