I have a site based on Codeigniter, and when I'm in the default directory, www.removed.com... I have no problem. If I go here: www.removed.com/ It works just fine.
The problem is when I navigate to the /admin folder (which is really just application/controllers/admin/dashboard.php..... routed correctly in the routes config.
If I add a trailing slash to the end of anything under admin (like removed.com/admin/users/), the whole site breaks and the url tries inserting a url relative to my root: http://www.removed.com/home/accountName/public_html/skc/admin/users
Any ideas?
Here's my .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
### Canonicalize codeigniter URLs
# If your default controller is something other than
# "welcome" you should probably change this
# RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
# RewriteRule ^(.*)/index/?$ $1 [L,R=301]
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
# Enforce www
# If you have subdomains, you can add them to
# the list using the "|" (OR) regex operator
# RewriteCond %{HTTP_HOST} !^(www|subdomain) [NC]
# RewriteRule ^(.*)$ http://www.domain.tld/$1 [L,R=301]
# Enforce NO www
# RewriteCond %{HTTP_HOST} ^www\.removed\.com [NC]
# RewriteRule (.*) http://removed.com/$1 [R=301,L]
###
# Removes access to the system folder by users.
# Additionally this will allow you to create a System.php controller,
# previously this would not have been possible.
# 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^ci.*
RewriteRule ^(.*)$ index.php/$1 [L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php
</IfModule>
Here's the routes config (as requested):
$route['default_controller'] = "main";
$route['404_override'] = '';
$route['about'] = "main/about";
$route['news'] = "main/news";
$route['bikes'] = "main/bikes";
$route['gallery'] = "main/gallery";
$route['contact'] = "main/contact";
$route['admin'] = "admin/dashboard";
$route['admin/login'] = "admin/dashboard/login";
$route['admin/logout'] = "admin/dashboard/logout";
$route['admin/users/view-user'] = "admin/users/view_user";
$route['admin/users/view-user/:num'] = "admin/users/view_user";
Change this condition to exclude the admin directory:
RewriteCond %{REQUEST_URI} ^ci.*
Additionally, you're currently using a blacklist approach here, where a whitelist one would be more effective. So, if you for example have all your images, css and javascript files in an 'assets' directory, if we assume that this is everything that needs to be accessible from the outside world, you should do this:
RewriteCond %{REQUEST_URI} !^(index\.php|assets\/.*\..*)
Related
Ok, i fixed all the problem, configured the server on localhost, but i want to hide the /public directory and want to display the content in the root folder of my document for example /forum/ and for logging /forum/login instead of going to forum/public/login. Here is the htaccess file but it dont work, because it gives No routes for GET /forum/.
RewriteEngine On
RewriteBase /forum/
RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
What im doing wrong ?
Actually sometimes we may need to add .htaccess file
We can use this code as .htaccess file. I have checked this on Ubuntu VPS and also on Cpanel.
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /index.php/
</IfModule>
</IfModule>
My root .htaccess looks like this, I think you can put it onto your symfony root folder.
# Removes index.php from ExpressionEngine URLs
RewriteEngine on
# RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*) /public/$1 [L]
Then my .htaccess inside the public/ folder:
# Determine the RewriteBase automatically and set it as environment variable.
# If you are using Apache aliases to do mass virtual hosting or installed the
# project in a subdirectory, the base path will be prepended to allow proper
# resolution of the index.php file and to redirect to the correct URI. It will
# work in environments without path prefix as well, providing a safe, one-size
# fits all solution. But as you do not need it in this case, you can comment
# the following 2 lines to eliminate the overhead.
RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::\2$
RewriteRule .* - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by Apache
RewriteCond %{HTTP:Authorization} .+
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]
# Redirect to URI without front controller to prevent duplicate content
# (with and without `/index.php`). Only do this redirect on the initial
# rewrite by Apache and not on subsequent cycles. Otherwise we would get an
# endless redirect loop (request -> rewrite to front controller ->
# redirect -> request -> ...).
# So in case you get a "too many redirects" error or you always get redirected
# to the start page because your Apache does not expose the REDIRECT_STATUS
# environment variable, you have 2 choices:
# - disable this feature by commenting the following 2 lines or
# - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
# following RewriteCond (best solution)
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
# Rewrite all other queries to the front controller.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ %{ENV:BASE}/index.php [L]
I am working on a project in which url Rewriting was done. Here is the link of the old uri
http://www.mysite.pk/jobs/search/faisalabad/all-cats
But now in the new url I have changed it to
http://www.mysite.pk/jobs/pakistan/faisalabad/all-cats
what I want to do is to change the redirect all the url's whcih have http://www.mysite.pk/jobs/search/ search after job in them to the
http://www.mysite.pk/jobs/pakistan/
Here is my code of the route
$route["jobs/pakistan"]="vacancies/search";
I have also done
$route["search/pakistan"]="vacancies/search";
My .htaccess file content is
but it is not working
Kindly Helpe me
Thanks in advance
I think the problem are the missing placeholders. Without the placeholders your stated URL
http://www.mysite.pk/jobs/search/faisalabad/all-cats
won't match.
The correct route should look like this:
$route["jobs/pakistan/(:any)"]="vacancies/search";
$route["search/pakistan/(:any)"]="vacancies/search";
This way both jobs/pakistan and search/pakistan are working.
Edit
This RewriteRule will have the same effect when put in the .htaccess file.
RewriteRule ^jobs/search/(.*)?$ http://www.mysite.pk/jobs/pakistan/$1 [L,R=301]
Your .htaccess file then should look similar to this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
###
RewriteRule ^jobs/search/(.*)?$ http://www.mysite.pk/jobs/pakistan/$1 [L,R=301]
# Removes access to the system folder by users.
# Additionally this will allow you to create a System.php controller,
# previously this would not have been possible.
# 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php
</IfModule>
The RewriteRule has following scheme:
RewriteRule [output] [original]
In this case when the "output" (url) starts with jobs/search/ (the ^ marks the beginning and $ the end) everything after this will be redirected to the original. The (.*) marks a reference for any content and will be inserted in the origin in place of $1. The [L,R=301] marks the type of the redirect (the HTTP response status code 301 means Moved Permanently), the L means that after the route is met, the following lines won't be processed.
I am trying to write an .htaccess file that accomplishes the following goals:
Strip trailing slash at the end of all URLS
Drop the file name for index.php
Drop the .php extension for everything that isn't the index
Keep all trailing data in URLs to be processed by index.php
Use relative URLs, without a link to the site root since the .htaccess only affects a subfolder (/admin as in the examples below)
Some example URLS (display URL = actual location):
/admin = /admin/index.php
/admin/pagename = /admin/pagename.php
/admin/directory = /admin/directory/index.php
/admin/one/two/three = /admin/index.php where I can parse $_SERVER['REQUEST_URI'] with PHP to get the variables one, two, and three
And here's what I have so far in the folder ABOVE admin. It removes the php extension while allowing me to access any existing files, but it also strips everything after .php and doesn't remove trailing slashes.
RewriteEngine On
# Redirect php filename externally
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L,QSA]
# change 302 to 301 on live for perm redirect
# Redirect php filename internally
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L,QSA]
# Rewrite Admin URLS (after specififcally looking for files)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule admin/. admin/index.php [L,QSA]
# index.php looks at $_SERVER['REQUEST_URI'] to decide what to do next
# No Directory Listings
Options -Indexes
My biggest concern is being able to include files (.php, .inc, .css, .js) with relative paths and not having to use absolute paths if I can help it.
I generally prefer to go with a framework that provides routing capabilities instead of the nightmare of all those Rewrite rules for folders/files. There are a slew of PHP frameworks that support this easily, such as Silex, Symfony, or Laravel, to name a few.
Another reason that doing the routing in the application is easier, is that it abstracts application routing from the webserver for the most part. For example, if you move to Nginx, you will have to refactor all of the above.
My .htaccess might look something like the following:
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect to non-trailing slash
RewriteRule ^(.*)/$ /$1 [R=301,L]
# Block access to "hidden" directories whose names begin with a period.
RewriteRule "(^|/)\." - [F]
RewriteBase /
# Redirect requests that have no real file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php [QSA,L]
</IfModule>
I am using Ubuntu 12.10. And I am having trouble removing the "index.php" from my codeigniter site URL.
My .htaccess files looks like this, I have my code inside hmvcExample folder.
*
RewriteEngine On
RewriteBase /hmvcExample/
### Canonicalize codeigniter URLs
# If your default controller is something other than
# "welcome" you should probably change this
RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
# Enforce www
# If you have subdomains, you can add them to
# the list using the "|" (OR) regex operator
RewriteCond %{HTTP_HOST} !^(www|subdomain) [NC]
RewriteRule ^(.*)$ http://www.domain.tld/$1 [L,R=301]
# Enforce NO www
#RewriteCond %{HTTP_HOST} ^www [NC]
#RewriteRule ^(.*)$ http://domain.tld/$1 [L,R=301]
###
# Removes access to the system folder by users.
# Additionally this will allow you to create a System.php controller,
# previously this would not have been possible.
# 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php
*
And my I have removed "index.php" from config.php as well. But I still get the following error when i try to load the site without "index.php"
Not Found
The requested URL /hmvcExample/signup was not found on this server.
Apache/2.2.22 (Ubuntu) Server at localhost Port 80
Please Help. Thanks.
This seems to be one of the question should have been looking for - How to remove "index.php" in codeigniter's path
And there is another config that you need to edit:
$config['uri_protocol'] = 'AUTO';
Change the line written above to the following:
$config['uri_protocol'] = 'REQUEST_URI';
If you have done all of this, there still might be one issue left - enable mod_rewrite
MAKE sure you have AllowOverride All set up in the /etc/httpd/conf/httpd.conf (centos apache 2.2)
config:
$config['index_page'] = '';
I have been working on my own mvc framework to further my web app learning, but am having trouble serving static resources. I am trying to have a single entry point into the application, aka a front controller, so in my project / I have an .htaccess file that redirects all requests to the app/ folder where another .htaccess passes the request uri to index.php (in app/) who delegates the request to the appropriate controllers.
However, when I try to serve up static content, such as javascripts or cascading style sheets, I still get redirected through app/index.php. I am also getting "favicon.ico does not exist in /var/www" errors in /var/log/apache2/errors.log (maybe because of symlink to ~/www?). I do not expect to because of the following .htaccess file in the root directory of my project root:
<IfModule mod_rewrite.c>
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
# ----------------------------------------------------------------------
# Suppress the "www." at the beginning of URLs
# ----------------------------------------------------------------------
# The same content should never be available under two different URLs - especially not with and
# without "www." at the beginning, since this can cause SEO problems (duplicate content).
# That's why you should choose one of the alternatives and redirect the other one.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
#----------------------------------------------------------------------
# Route static resources to respective files
#----------------------------------------------------------------------
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond public/$0 -f
RewriteRule ^.+\.(jpg|gif|png|ico|css|js)$ /public/$0 [L]
#----------------------------------------------------------------------
# Redirect all other requests to the app folder
#----------------------------------------------------------------------
RewriteRule ^$ app/ [L]
RewriteRule (.*) app/$1 [L]
</IfModule>
and here is the .htaccess in my app/ folder:
<IfModule mod_rewrite.c>
RewriteEngine On
# ensure request is not path to filename or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# redirect all requests to index.php?url=PATHNAME
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Why can't I serve static content correctly? This would make sense to me if I wasn't trying to sent all static requests to public/, which is where my css, jpg, png, js, etc files reside. But I have a RewriteCond rule in there to send the requests for such files to the public dir... Confusing?
Assuming, from what I understood, that your project structure is the following:
/
/.htaccess
/app/.htaccess
/app/index.php
/public/static.js (for example)
Here is what I come up with, hoping it'll solve your problem:
the .htaccess in the root folder:
<IfModule mod_rewrite.c>
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteRule ^public/.+\.(jpg|gif|png|ico|css|js)$ - [L]
RewriteRule ^(.*)$ app/$1 [L]
</IfModule>
And the .htaccess in the app folder is unchanged.
Every request starting with public and being a file with the listed extensions won't be redirected which is done with the dash character.
The last rule allows to redirect a request to the app/index.php file.
I think the resulting behaviour is the expected one:
static files in the public directory are not redirected,
files with another extension in the public directory will be
redirected to app/index.php (maybe for some error treatment),
requests not starting with public will be redirected to
app/index.php.