Error installing another PHP app in subfolder of SilverStripe - .htaccess

I have Silverstripe installed on www.mywebsite.com (I made that up just as an example)
I am testing Wordpress on www.mywebsite.com/test
Here is my .htaccess file in my root folder:
### SILVERSTRIPE START ###
# Deny access to templates (but allow from localhost)
< Files *.ss >
Order deny,allow<br>
Deny from all<br>
Allow from 127.0.0.1
< /Files >
# Deny access to IIS configuration
< Files web.config >
Order deny,allow
Deny from all
< /Files >
# Deny access to YAML configuration files which might include sensitive information
< Files ~ "\.ya?ml$">
Order allow,deny
Deny from all
< /Files>
# Route errors to static pages automatically generated by SilverStripe
ErrorDocument 404 /assets/error-404.html
ErrorDocument 500 /assets/error-500.html
< IfModule mod_env.c>
# Ensure that X-Forwarded-Host is only allowed to determine the request
# hostname for servers ips defined by SS_TRUSTED_PROXY_IPS in your _ss_environment.php
# Note that in a future release this setting will be always on.
SetEnv BlockUntrustedIPs true
< /IfModule>
< IfModule mod_rewrite.c>
# Turn off index.php handling requests to the homepage fixes issue in apache >=2.4
< IfModule mod_dir.c>
DirectoryIndex disabled
< /IfModule>
SetEnv HTTP_MOD_REWRITE On
RewriteEngine Off
# Enable HTTP Basic authentication workaround for PHP running in CGI mode
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Deny access to potentially sensitive files and folders
RewriteRule ^vendor(/|$) - [F,L,NC]
RewriteRule silverstripe-cache(/|$) - [F,L,NC]
RewriteRule composer\.(json|lock) - [F,L,NC]
# Process through SilverStripe if no file with the requested name exists.
# Pass through the original path as a query parameter, and retain the existing parameters.
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* framework/main.php?url=%1 [QSA]
# If framework isn't in a subdirectory, rewrite to installer
RewriteCond %{REQUEST_URI} ^(.*)/framework/main.php$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . %1/install.php? [R,L]
< /IfModule><br>
### SILVERSTRIPE END ###
< IfModule pagespeed_module>
ModPagespeed Off
</IfModule>
< IfModule mod_expires.c><br>
ExpiresActive Off<br>
< /IfModule>
Code on /test/.htaccess:
< IfModule mod_rewrite.c><br>
SetEnv HTTP_MOD_REWRITE On<br>
RewriteEngine On<br>
RewriteBase /<br>
RewriteCond %{REQUEST_URI} !/test<br>
RewriteEngine On<br>
RewriteCond %{REQUEST_FILENAME} !-f<br>
RewriteCond %{REQUEST_FILENAME} !-d<br>
RewriteRule . /index.php [L]<br>
< /IfModule>
Any ideas? Note: The < br > does not show up in the actual file and there are no spaces between the < and >
I get a list of all my files. My index.php file doesn't even show up properly and the URL changes from /test to test/?url=/test
I use zoom.ph shared hosting.
Update:
My new code in the root .htacccess file:
RewriteEngine On
# Change any direct URLs (www.unclebubby.com...) to the subdomain (wavs.unclebubby.com)
RewriteCond %{HTTP_HOST} ^(www\.)?collegeconnect\.ph$ [NC]
RewriteCond %{REQUEST_URI} ^/test/(.*)$ [NC]
RewriteRule .* http://test.collegeconnect.ph/%1 [R=301,L]
RewriteCond %{REQUEST_URI} !/test
# If there is a .htm at the end of the URL, get rid of it (due to migration of site from FrontPage)
RewriteCond %{REQUEST_URI} ^/(.+)\.htm$ [NC]
RewriteRule ^.+\.htm$ /%1 [R=301,L]
RewriteBase /
### SILVERSTRIPE START ###

I tested this and what you have seems to work fine. Just ensure you have no errors in your htaccess. You can check apache.log for this.
Here's what I used:
DirectoryIndex index.html index.php
<IfModule mod_rewrite.c>
SetEnv HTTP_MOD_REWRITE On
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !/test
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This redirected me successfully to the Wordpress setup page (fresh install).

First, your RewriteBase in test/.htaccess is wrong. It's not "/" (root), but "/test/" (as files are there).
Second, you have "RewriteEngine On" twice in your test/.htaccess - which may not be an error, but not needed anyway.
Third - your test/.htaccess file is literally disabled by itself by line:
RewriteCond %{REQUEST_URI} !/test
which means - all requests that don't have "test" in URL, which no request to "test" folder can fullfil. Your test/.htaccess should look like this:
< IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
< /IfModule>
BONUS: Silverstripe scans entire folder where it is installed and adds those classes to its manifest, which means you should exclude wordpress (ie folder "test") by placing empty file called "_manifest_exclude" in it.

mention folder name in 'RewriteBase' like below in .htaccess file at your root path
RewriteBase '/SilverStripe/'
where SilverStripe be ur folder name like u mentioned
RewriteBase '/test/'
please do remember to add slash before and after the folder name. If it doesnt work , then let me know. Thanks

Related

.htaccess redirect URL with specific query string parameter, compatibility with multiple servers

I want to rewrite this specific URL:
https://www.domain.test/cart/?query_action=abc
...to this URL:
https://www.domain.test/cart/
The "abc" part is variable, everything else is exactly what I want to rewrite. I added these lines to my .htaccess file:
RewriteCond %{QUERY_STRING} ^query_action\=
RewriteRule ^folder/$ /folder/? [L]
When I run this on my local environment, it works. But when I upload to our development server (WP Engine), it does not work. Are there incompatibilities with .htaccess between different servers? Does my code's proximity with WordPress' default code have anything to do with it? Is there something I can tweak in my rewrite to improve it?
Here is my entire .htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^query_action\=
RewriteRule ^folder/$ /folder/? [L]
# 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
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
<Files xmlrpc.php>
order deny,allow
deny from all
</Files>

Installing Codeigniter 4 in a subdirectory and keep the domain general URL structure

Current situation:
I already have a Wordpress installation at http://example.com and this site is multilanguage so you can access a web page in English at http://example.com/en/something-here/ and you can access the Spanish version of this webpage at http://example.com/es/algo-aqui/.
What I am trying to do:
I want to install Codeigniter 4 in http://example.com/software/ but I want to keep the general website URL structure, so I need to be able to access the CI4 software through the URL http://example.com/en/software/ and http://example.com/es/software/. Always reading the same CI4 installation mentioned at the beginning of this paragraph.
What I already tried:
I deployed the CI4 files at http://example.com/software/ and modified the .htaccess files of both, the Wordpress site and Code Igniter.
This is my WP .htaccess file on the root directory:
# Disable directory browsing
Options All -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[a-z]{2}/software(.*)$ /software/index.php$2 [L]
</IfModule>
# BEGIN WordPress
# Las directivas (líneas) entre `BEGIN WordPress` y `END WordPress` se generan dinámicamente
# , y solo se deberían modificar mediante filtros de WordPress.
# Cualquier cambio en las directivas que hay entre esos marcadores se sobreescribirán.
<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
And this is my .htaccess file on the "software" directory:
# Disable directory browsing
Options All -Indexes
# ----------------------------------------------------------------------
# Rewrite engine
# ----------------------------------------------------------------------
# Turning on the rewrite engine is necessary for the following rules and features.
# FollowSymLinks must be enabled for this to work.
<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
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 (.*) /software/index.php/$1
# 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
The error I am getting:
I can access both, the site and the software. The site works well but when I try to access let's say http://example.com/en/software/, I get a 404 error when I access Codeigniter. The error message says: "Controller or its method is not found: App\Controllers\En::software".
This is as if CI is trying to find the controller En (that not exist) and the method software.
Any help will be appreciated.
I have solved the problem after trying for 2 days relentlessly. Here's the steps I actually followed-
First Backup the Codeigniter4 Application by giving the below command as root:
tar --exclude=".git" -czvf zipped_file_name.tar codeigniter4_project_folder/
upload the codeigniter4_project_folder in public_html directory at Shared Hosing and change the following:
Change the base url in app/Config/App.php or .env file in root folder of codeigniter application (here ci4 is the subdomain which will be configured in cpanel later)
app.baseURL = 'http://ci4.yourdomain.com/'
Change the Database Config in app/Config/Database.php or .env file in root folder
database.default.hostname = localhost
database.default.database = your_db_name
database.default.username = your_db_user
database.default.password = your_db_password
database.default.DBDriver = MySQLi
Change .htaccess file in Public Folder
RewriteBase /codeigniter4_project_folder/
Add the Subdomain in Cpanel ci4.yourdomain.com which path will be as follows:
public_html/codeigniter4_project_folder/public
Here public folder will be the last in the path. It is very important.
Hope this may help others struggling with the problem.
In my case I needed to add:
RewriteRule ^(.*)$ index.php?/$1 [L]
in public/.htaccess.

.htaccess redirect & hide directory path in url

I have wordpress installed in root directory. I want to use some separate php files as a page on my domain, for which I have made a separate directory which holds all files for serving the php files, of which the directory structure is as:
the root folder has all the wordpress files
the directory which I'd like to serve as a page
/inc/css/
/inc/php/
/inc/img/
the CSS stylesheet files directory location in the PHP file is ../inc/css one step back & then css folder.
I want to hide folders from URL such as the files are served from the root (hide the /inc/php/, /inc/css/ & /inc/img/ folders from URL).
eg: www.domain.com/inc/php/about.php redirect & rewrite this URL to www.domain.com/about
the .htaccess in my root
RewriteEngine On
RewriteBase /
# disable directory browsing
Options -Indexes
# Prevent hotlinking of images htaccesstools.com/hotlink-protection
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?domain.com [NC]
RewriteRule \.(jpg|jpeg|png|gif|ico|pdf|flv|jpg|jpeg|mp3|mpg|mp4|mov|wav|wmv|png|gif|swf|css|js)$ - [NC,F,L]
RewriteRule ^login$ http://domain.com/wp-login.php [NC,L]
# BEGIN WordPress
<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
<files wp-config.php>
order allow,deny
deny from all
</files>
<files ~ "^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
satisfy all
</files>
I have tried the simple redirect rule but the folders are exposed in the URL.
Redirect 301 /about.php /inc/php/about.php
also I have some more files in the PHP folder on which I'd like to apply the same rule of redirect & rewrite URL & hide folders from URL & remove the PHP extention.
www.domain.com/inc/php/about.php redirect & rewrite this URL to www.domain.com/about
This, of course, means that you can't have the same base filename that's a php file and, say, a css file. Since if the request is www.domain.com/about, is that supposed to map to /inc/php/about.php or /inc/css/about.css? Or is it an image? If you have both files, only one will get mapped to.
But if that's really what you want, try adding these rules right after the hotlinking rule that you have:
# Externally redirect requests for /inc/
RewriteCond %{THE_REQUEST} \ /inc/(php|img|css)/([^\?\ ]+)\.(php|css|jpe?g|png|gif) [NC]
RewriteRule ^ /%2 [L,R=301]
# Check if the request is a php file:
RewriteCond %{DOCUMENT_ROOT}/inc/php%{REQUEST_URI}.php -f
RewriteRule ^(.*)$ /inc/php/$1.php [L]
# Check if the request is a css file:
RewriteCond %{DOCUMENT_ROOT}/inc/css%{REQUEST_URI}.css -f
RewriteRule ^(.*)$ /inc/css/$1.css [L]
# Check if the request is a jpeg file:
RewriteCond %{DOCUMENT_ROOT}/inc/img%{REQUEST_URI}.jpg -f
RewriteRule ^(.*)$ /inc/img/$1.jpg [L]
# Check if the request is a gif file:
RewriteCond %{DOCUMENT_ROOT}/inc/img%{REQUEST_URI}.gif -f
RewriteRule ^(.*)$ /inc/img/$1.gif [L]
# Check if the request is a png file:
RewriteCond %{DOCUMENT_ROOT}/inc/img%{REQUEST_URI}.png -f
RewriteRule ^(.*)$ /inc/img/$1.png [L]

Kohana rewriting urls in .htaccess showing file not found message

I'm using Kohana 3.2 framework, I've put the standard .htaccess file in the same folder of the aplication.
my .htaccess file is
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /mysite/
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ /index.php/$0 [PT]
I've tried changing the las line to
RewriteRule ^(.*)$ /index.php/$0
RewriteRule ^(.*)$ /index.php/$0 [PT,L]
RewriteRule ^(.*)$ /index.php/$0 [PT,L,QSA]
RewriteRule .* /index.php/$0 [PT] - [PT,L] - [PT,L,QSA]
But it still shows
Not Found
The requested URL /index.php/login was not found on this server.
The complete path of the app is /var/www/es/ec/mysite
The complete working URL is http://10.0.0.1/ec/mysite/index.php/login
The complete NOT working URL is http://10.0.0.1/ec/mysite/login
Also...
Running in Apache 2.2.3 over CentOS 5
Any idea???
I got it!!
Here is how it gets done when the app is inside other folders.
The complete path of the app is /var/www/es/ec/mysite
The complete working URL is http://10.0.0.1/ec/mysite/index.php/login
The complete NOT working URL was http://10.0.0.1/ec/mysite/login
In /var/www/es/ec/mysite/application/bootstrap.php the Kohana::init stays like this
Kohana::init(array(
'base_url' => '/ec/mysite/',
'index_file' => FALSE,
'charset' => 'ISO-8859-1',
));
And the .htaccess like this
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* /ec/mysite/index.php/$0
Hope this helps some one else!
Cheers!
You can try my Kohana .htaccess file
# Set environment
SetEnv KOHANA_ENV "development"
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I use Kohana 3.2, also. This rewrite tends to work for most of my projects.
Let me know how it works out!
In your bootstrap.php inside Kohana::init set 'index_file' => '',

mod_rewrite, can't rewrite existing file

I'm trying to pass all images in a directory to watermark.php using mod_rewrite in .htaccess.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.jpg$ /watermark.php?path=%{REQUEST_FILENAME} [L]
It works fine on local machine, but on my online production server (shared hosting) all image files are served without rewriting.
mod_rewrite is enabled online, but it ignores the rule if file exists.
What could be wrong?
UPDATE
Here's my full setup: there's a domain with a subdomain in a subfolder of main domain's document_root.
public_html (example.com DOCUMENT_ROOT)/
img (img.example.com DOCUMENT_ROOT)/
.htaccess in public_html folder:
<FilesMatch "\.(inc\.php|log)$">
Deny from all
</FilesMatch>
Options -Indexes +FollowSymLinks
php_value short_open_tag 0
php_value auto_prepend_file /home/username/public_html/bootstrap.inc.php
RewriteEngine On
RewriteCond %{HTTP_HOST} ^img\. [NC]
RewriteRule .* - [L]
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example\. [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ shop/$1 [L]
.htaccess in img folder:
<FilesMatch "\.(inc|log)$">
Deny from all
</FilesMatch>
Options -Indexes +FollowSymLinks
php_value short_open_tag 0
php_value auto_prepend_file none
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.jpg$ /watermark.php?path=%{REQUEST_FILENAME} [L]
The problem is that existing images in img folder are not rewrited. Apache just serve them as is. But if, for example, I request non-existing file the rule works and echo $_GET['path'] prints the full filepath to non-existing file.
Here's what you can use:
RewriteEngine On
RewriteRule ^(media|image|images)(/?)(.*)$ - [F]
RewriteRule ^([a-z0-9\-_\.]+)\.(jpg|jpeg|png|gif)$ /watermark.php?path=%{REQUEST_URI} [L]
The first RewriteRule is set to make sure people don't go to that folder - it gives a 403 permission denied (this is optional)
The second one will redirect any images that match letters, numbers, dash, underscore and dot with the extension: jpg, jpeg, png and gif to your watermark.php file. eg:
http://www.domain.com/logo.jpg => path = /logo.jpg
UPDATE:
Since the rule is in a different folder and the root directory of IMG is /home/username/public_html/img/ the watermark.php file does not exists. Either copy the file you need (watermark.php and its libraries) to the IMG folder or create symblink.
cd /home/username/public_html/img/
ln -s ../watermark.php (and other library files as well)
htaccess change :
Options -Indexes +FollowSymLinks
by
Options -Indexes +FollowSymLinks -MultiViews
This behaviour steel exists on Apache 2.4..

Resources