I would like to map "http://www.example.com/abc" to "http://www.example.com/test/abc" for having the shortest route possible. I am using pyroCMS for my users and content.
the default pyrocms file:
# Multiple Environment config
# Set this to development, staging or production
# SetEnv PYRO_ENV production
<IfModule mod_rewrite.c>
# Make sure directory listing is disabled
Options +FollowSymLinks -Indexes
# disable the Apache MultiViews directive if it is enabled on the server. It plays havoc with URL rewriting
Options -MultiViews
RewriteEngine on
# Keep people out of codeigniter directory and Git/Mercurial data
RedirectMatch 403 ^/.*/(vendor|composer\.json|composer\.lock|system/cms/cache|system/codeigniter|system/cms/config|system/cms/logs|\.git|\.hg).*$
# Send request via index.php (again, not if its a real file or folder)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
<IfModule mod_php5.c>
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_php5.c>
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
</IfModule>
I would like to add this rule to the file:
RewriteRule ^([a-z0-9_]+)$ test/$1
To rewrite "BASE_URL/abc" to "BASE_URL/test/abc"
However,
i tried many positions as to where to put this RewriteRule, my website keeps giving a "Page Missing".
Is my RewriteRule ok? And where do i insert it?
PyroCMS has built-in modular routing ability. see here:
http://docs.pyrocms.com/2.2/manual/developers/basics/modular-routing
If your "http://www.example.com/abc" refers to a custom module,then, you can add a file named "routes.php" in a in config folder of your module.
the folder construction should like this :
addones/shared_addons/modules/your-module/config/routes.php
OR even you can edit the core route config file located at system/cms/config/routes.php and add this line or whatever your routing rules are:
$route['abd'] = 'test/abd';
OR more even, at your control pannel there is a redirect module that you can add redirections.
It all depends on what your rule is supposed to do and how it is supposed to interact (or not interact) with the rest of your site. And considering your entire htaccess file is mostly commented out code (which I removed to make it halfway readable), you just want to place it under RewriteEngine On.
However, since it blindly routes everything into test you need to add a few conditions and make it something lilke:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/test%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/test%{REQUEST_URI} -d
RewriteRule ^([a-z0-9_]+)$ test/$1 [L]
Related
I have a subdomain called test.mysite.com and I have a CI4 installation inside a folder there called project. So the actual url for the CI4 installation is test.mysite.com/project/public/index.php. I want to remove the public/index.php portion from the url but continue to use the public folder to have my files, as they should.
I'm using this .htaccess on the project folder root:
DirectoryIndex /public/index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|assets|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./public/index.php/$1 [L]
<IfModule mod_headers.c>
<FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
</IfModule>
But it's not working. When I access test.mysite.com/project it leads me to a server list of files. I know the .htaccess file is being properly read because when I add an error there it gives me a warning
EDIT:
CI4 already comes with an htaccess inside the public folder:
# Disable directory browsing
Options All -Indexes
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# If you installed CodeIgniter in a subfolder, you will need to
# change the following line to match the subfolder you need.
# http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
# RewriteBase /
# Redirect Trailing Slashes...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Rewrite "www.example.com -> example.com"
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,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 the front controller, index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA]
# Ensure Authorization header is passed along
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
ErrorDocument 404 index.php
</IfModule>
# Disable server signature start
ServerSignature Off
# Disable server signature end
When I access test.mysite.com/project it leads me to a server list of files
Because your DirectoryIndex is set to /public/index.php (which presumably does not exist, as the index document is located at /project/public/index.php) and directory indexes (mod_autoindex) is presumably enabled (it should be disabled, so that such a request results in a 403 Forbidden).
the difference is that the other website that is working is not on a subdomain and it’s on the root, so it’s not the same htaccess
I'm not sure why it would be any different?
With the .htaccess file in the /project subdirectory, arrange your mod_rewrite (and mod_dir) directives like this instead:
DirectoryIndex index.php
Options -Indexes
RewriteEngine on
RewriteCond $1 !^public/(index\.php|images|assets|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) public/index.php/$1 [L]
The presence of robots\.txt and favicon\.ico in the first condition implies you are rewriting requests from the document root. Since search engines request /robots.txt (in the document root), not /project/robots.txt (or /project/public/robots.txt). The same applies to /favicon.ico. If you are not rewriting these two requests then these two entries are not required.
This also assumes you are linking directly to your static resources using the public subdirectory. eg. /projects/public/images/foo.jpg. This isn't necessarily desirable since it exposes /public in the URL path. Users won't necessarily see this as it's not directly visible in the browser's address bar, but search engines and anyone who views the HTML source / network traffic will see it.
Just to add, that first condition (ie. RewriteCond directive) is "just" an opimisation. If it's set incorrectly, your site will probably work OK and you won't see a difference, except that it will be performing many more filesystem checks than it needs to do.
Alternative structure
An alternative approach is to have two .htaccess files. A basic /project/.htaccess file that simply rewrites everything to the public subdirectory and a more comprehensive (CI) .htaccess file at /project/public/.htaccess that actually routes the request to CI. This then allows you to omit public from all URLs, including URLs to your static resources.
For example:
/project/.htaccess
DirectoryIndex index.php
Options -Indexes
RewriteEngine On
# Unconditionally rewrite everything to the "public" subdirectory
RewriteRule (.*) public/$1 [L]
/project/public/.htaccess
The presence of mod_rewrite directives in the subdirectory .htaccess file naturally prevent a rewrite loop from the RewriteRule directive in the parent directory. (Assuming mod_rewrite inheritance has not been enabled.)
RewriteEngine on
RewriteCond $1 !^(index\.php|images|assets|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php/$1 [L]
<IfModule mod_headers.c>
<FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
</IfModule>
Working for years with Codeigniter 3 I had this issue too. First I tried the .htaccess road but after realizing Codeigniter 3 could also benefit from a more secure structure, I applied the same experience to Codeigniter 4.
The basic idea is to move the whole framework to a folder off the web root. And move the public folder to the root (WEBROOT can also be a subfolder under the public html folder)
PRIVATEFOLDER
\codeigniter:
\app
\vendor
\writable
WEBROOT
assets\
index.php
.htaccess
Then I'll modify index.php (and spark and preload.php if used). This will do in index.php:
// This is the line that might need to be changed... etc
define('ENGINEPATH', 'PRIVATEFOLDER/codeigniter');
require ENGINEPATH . '/app/Config/Paths.php';
And /app/Config/Paths.php to this:
namespace Config;
class Paths
{
public $systemDirectory = ENGINEPATH . '/vendor/codeigniter4/framework/system';
public $appDirectory = ENGINEPATH . '/app';
public $writableDirectory = ENGINEPATH . '/writable';
public $testsDirectory = ENGINEPATH . '/tests';
public $viewDirectory = ENGINEPATH . 'app/Views';
}
Now, set the $baseURL to the WEBROOT url and you should be able to navigate without index.php and public, and the app code is protected outside the public folder.
I’m trying to deploy a basic webapp on a shared environment where Wordpress is on the root. The Yii2 app is in /subfolder.
I’m following this guide. In root’s .htaccess I added:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /subfolder
RewriteCond %{REQUEST_URI} ^/subfolder
RewriteCond %{REQUEST_URI} !^/subfolder/web
RewriteRule ^assets/(.*)$ /web/assets/$1 [L]
RewriteRule ^css/(.*)$ /web/css/$1 [L]
RewriteRule ^js/(.*)$ /web/js/$1 [L]
RewriteRule ^images/(.*)$ /web/images/$1 [L]
RewriteRule (.*) /web/$1 [L]
RewriteBase /subfolder
RewriteCond %{REQUEST_URI} ^/subfolder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /web/index.php
</IfModule>
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
But with these rules added all Wordpress’ pages are handled (or attempted) through Yii, so this breaks the blog installation. It’s the first block of rules capturing all the pages, but I don’t understand why as the two RewriteCond should intercept only the Yii app URIs. I checked mod_rewrite docs but couldn’t understand what’s wrong. Any help is appreciated. Thanks
RewriteBase /subfolder
You cannot set multiple RewriteBase directives in the same .htaccess file. The last instance "wins" and controls the entire file. So, in the .htaccess file you posted, RewriteBase / set in the WordPress code block, is what is actually set for the file.
However, none of the directives actually make use of the RewriteBase directive anyway - so none of the RewriteBase directives are actually doing anything. The RewriteBase directive only applies where you have set a relative path (not starting with a slash) in the RewriteRule substitution string.
but I don’t understand why as the two RewriteCond should intercept only the Yii app URIs.
RewriteCond %{REQUEST_URI} ^/subfolder
RewriteCond %{REQUEST_URI} !^/subfolder/web
RewriteRule ^assets/(.*)$ /web/assets/$1 [L]
Presumably it's these two RewriteCond directives you are referring to... in which case these two conditions aren't really doing anything. RewriteCond directives only apply to the first RewriteRule directive that follows, so it only applies to the directive that rewrites your assets.
However, this RewriteRule is matching /assets in the document root, not /subfolder/assets, which is presumably the requirement - so these rules will fail to match.
But with these rules added all Wordpress’ pages are handled (or attempted) through Yii, so this breaks the blog installation.
The rules will certainly "break the blog installation", however, they don't appear to get as far as handling the request "through Yii". There's nothing that actually rewrites the request to /subfolder. However, the following directive unconditionally rewrites everything to the /web directory in the document root (which presumably does not exist) - so this will certainly "break" all the WordPress URLs.
RewriteRule (.*) /web/$1 [L]
In fact, I would have expected this to have created a rewrite-loop (500 Internal Server Error response)?! Unless you have a subdirectory /web off the document root which also contains an .htaccess file containing mod_rewrite directives? But that seems unlikely, since the /web directory should be inside the /subfolder directory?
Try the following instead:
RewriteEngine On
RewriteRule ^(subfolder)/(assets|css|js|images)/(.*) $1/web/$2/$3 [L]
RewriteRule ^(subfolder)/((?!web).*) $1/web/$2 [L]
RewriteRule ^subfolder/web/index\.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(subfolder)/. $1/web/index.php [L]
# BEGIN WordPress
:
No need for the <IfModule> wrapper. Or the RewriteBase directive.
Alternatively
However, it would be preferable to move these directives into their own .htaccess file in the root of the project, ie. /subfolder/.htaccess - which I believe is what the linked "guide" is suggesting. This keeps the two projects entirely separate. And avoids having to explicitly include the /subfolder in the directives.
In addition, creating a another .htaccess file in the web subdirectory, ie. /subfolder/web/.htaccess. This is again, suggested in the linked "guide". However, this also negates the need for the additional directives to route the request in the parent .htaccess file.
For example, putting these changes together, the /.htaccess file in the document root should only have the WordPress directives. And then...
/subfolder/.htaccess
RewriteEngine On
RewriteRule ^((?:assets|css|js|images)/.*) web/$1 [L]
RewriteRule ^((?!web).*) web/$1 [L]
/subfolder/web/.htaccess
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Again, no need for the RewriteBase directive here - in fact, using RewriteBase here arguably complicates things. When in the /subfolder/web/.htaccess file, all relative URL-paths are relative to that directory.
So, requesting /subfolder/foo gets internally rewritten by the /subfolder/.htaccess file to /subfolder/web/foo. Which is then caught by the /subfolder/web/.htaccess file (preventing a rewrite loop) and internally rewritten to /subfolder/web/index.php (providing foo does not exist as a physical file).
I'm relatively new to Codeigniter and MVC. But, have successfully made two apps 'Locally'. While exploring, I found a way to remove 'Index.php' from the URL and also about custom routes. The .htaccess file that i have works like charm locally, but when trying to host it; there is a issue 505 internal server issue
Here is the first .htaccess code that i have (works locally) :-
<IfModule mod_rewrite.c>
# Turn on URL rewriting
RewriteEngine On
# If your website begins from a folder e.g localhost/my_project then
# you have to change it to: RewriteBase /my_project/
# If your site begins from the root e.g. example.local/ then
# let it as it is
RewriteBase /
# Protect application and system files from being viewed when the index.php is missing
RewriteCond $1 ^(application|system|private|logs)
# Rewrite to index.php/access_denied/URL
RewriteRule ^(.*)$ index.php/access_denied/$1 [PT,L]
# Allow these directories and files to be displayed directly:
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|public|assets|css|js|images)
# No rewriting
RewriteRule ^(.*)$ - [PT,L]
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
</IfModule>
When i use the code above there is an error. And, the app works only when .htaccess is removed. (I then have to use the inconvenient long URLs)
After a brief research and using different .htaccess without success, i asked one of my friends who has a Hosted CI app successfully running. He sends me a file which leads me the landing page without any problem; but, cant call any functions with/without using routing .i.e. If i use the custom routed URl (www.mySite.com/contact) then also it leads me to the landing page, the same with actual URL scheme (www.mySite.com/welcome/contact_page)
The new code here:-
<IfModule mod_rewrite.c>
RewriteEngine On
# !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
# slashes.
# If your page resides at
# http://www.example.com/mypage/test1
# then use
# RewriteBase /mypage/test1/
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
Using this code shows me the landing page but i cannot navigate any further from there. When i try to call a function the landing page reloads. This, I think is because of the last error Handling Code (ErrorDocument 404 /index.php)
Does anyone know what the solution the problem might be??
questions
Why doesnt the first .htaccess code work when hosted?
What may be the issue with the second available .htaccess file??
Do you guys have better .htaccess file? If yes, can you post it here??
Try this .htaccess (Codeigniter Recommended )
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|image|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
or
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
In application/config/config.php
$config['base_url'] = 'http://stackoverflow.com/';
$config['index_page'] = ''; # Should be empty
And make sure Controller name, Model names are in proper way. Bcz Linux Host is an Case-Sensitive.
The following .htaccess was able to allow my symfony project domain to be served without app.php in the URL, as intended. The only issue is that it is breaking all other (non-symfony related) urls and causing an internal server error.
Right now, after deleting the .htaccess file from the server, all my domains work but the project tied to symfony must be accessed using app.php in URL ?
Is it possible to modify the below .htaccess to rewrite the symfony url to not require app.php in the URL while still allowing for all other URLs, not tied to symfony, to be accessed successfully?
Not sure this is needed, but assume the domain tied to symfony is www.apples.com.
Thanks in advance!
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# Explicitly disable rewriting for front controllers
RewriteRule ^app.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
# Change below before deploying to production
RewriteRule ^(.*)$ /app.php [QSA,L]
</IfModule>
Edit
I also tried the following:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# Explicitly disable rewriting for front controllers
RewriteRule ^app.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change below before deploying to production
RewriteRule ^(.*)$ app.php [QSA,L]
DirectoryIndex index.php
</IfModule>
Right after
RewriteCond %{REQUEST_FILENAME} !-f
add
RewriteCond %{REQUEST_FILENAME} !-d
and set DirectoryIndex to index.php
A bit late to this game but I just found an Apache directive that saves so much time and makes your htaccess cleaner. The only caveat is you must have Apache 2.2.16+. To have all urls except valid files (images, etc) use app.php as a front controller use the FallbackResource directive.
<Directory "/web/symfony-project">
FallbackResource /app.php
</Directory>
I've been trying to set up .htaccess to create clean PHP URL. My goal is to achieve clean URLs within the folder CL.
.htaccess:
# Use PHP5 Single php.ini as default
AddHandler application/x-httpd-php5s .php
<IfModule mod_rewrite.c>
# This is the initialization
# For security reasons, Option followsymlinks cannot be overridden.
#Options +FollowSymLinks
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
# /CL/var1/var2/var3
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^CL/([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/?$ /CL/index.php?var1=$1&var2=$2&var3=$3 [L,QSA]
# /CL/var1/var2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^CL/([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/?$ /CL/index.php?var1=$1&var2=$2 [L,QSA]
# /CL/var1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^CL/([a-zA-Z0-9\-]+)/?$ /CL/index.php?var1=$1 [L,QSA]
</IfModule>
However, for some reason, I get a 500 internal server error when y enter the folder CL. I've used similar .htaccess configurations in the past, but now its not working fore some reason.
Any ideas?
"I've used similar .htaccess configurations in the past, but now its not working fore some reason."
OK, so the ruleset works. I've just tested in on one my test VMs so it still works, so your problem isn't the mod_rewrite functionality in this .htaccess file. So where could it be?
Think laterally. A 500 status is usually a mod_rewrite loop. Have you got an .htaccess file in /CL? This could create a perdir indefinite loop.
Unfortunately DS suggestion of trying to get an answer on error_log content from a shared hosting provider for a $100 pa shared service is a no-no. Morale: set up a test VM on your PC to debug this stuff where you can get root access to the log as article here.