Uncaught SyntaxError: Unexpected token < in React JS - .htaccess

I have an .htaccess set up in http://example.com/testsite/ with the following commands:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
RewriteRule ^ index.html [L]
I am then trying to set up routing in my index.js file. It works fine if my route is just a single level such as http://example.com/testsite/page1
But if I try to connect to a route such as http://example.com/testsite/category/page1 then I get the following error:
Uncaught SyntaxError: Unexpected token < bundle.js:1
My route commands look like this:
<BrowserRouter>
<div>
<Switch>
<Route exact path={"/testsite"} component={Front} />
<Route path={"/testsite/page1"} component={Works} />
<Route path={"/testsite/category/page1"} component={NotWorks} />
<Route component={ErrorPage404} />
</Switch>
</div>
</BrowserRouter>
Even if I remove the third router path and just try to get it to display the error page I still get the same error. If I completely remove the Router code I still get the error. This leads me to believe that it is nothing to do with the router code itself. What else could be the problem?
UPDATE:
As requested, here is my webpack.config.js file:
var webpack = require('webpack');
var path = require('path');
var DIST_DIR = path.resolve(__dirname, 'dist');
var SRC_DIR = path.resolve(__dirname, 'src');
var config = {
entry: SRC_DIR + '/app/index.js',
output: {
path: DIST_DIR + '/public/js',
filename: 'bundle.js'
},
module: {
loaders: [
{
test: /\.jsx?$/,
include: SRC_DIR,
loader: 'babel-loader',
query: {
presets: ['es2015','react']
}
}
]
},
};
module.exports = config;
My .babelrc
{
"presets" : ["es2015", "react"]
}

I have tracked down the issue and found out that I needed to change the script line in my index.html file from this:
<script src="js/bundle.js" type="text/javascript"></script>
to this:
<script src="/testsite/js/bundle.js" type="text/javascript"></script>
The reason for this is that while on the backend the .htaccess does redirect all traffic to index.html, the path in the browser has been unchanged. Therefore when the browser tries to load the javascript file it is looking for it in the /testsite/category/ directory (which doesn't technically exist) instead of looking for it in the /testsite/ directory.

I've met the same problem.
In my case it is ok with next .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
</IfModule>
And changed publicPath for prod from "" to "/" in webpack.config.js like:
output: {
path: argv.mode === "production" ? __dirname + '/prod' : __dirname + '/dist',
publicPath: '/',
filename: '[hash].bundle.min.js'
},

Related

.htaccess Pretty URL with languages

I have a php website, loading the pages as below.
if ( $secretfile && file_exists( $moddir . $name . "/" . $secretfile . ".php" ) ) {
include_once( $moddir . $name . "/" . $secretfile . ".php" );
} else {
if ( isset( $_GET[ 'mod' ] ) ? $_GET[ 'mod' ] : '' ) {
$secretmodule = isset( $_GET[ 'mod' ] ) ? $_GET[ 'mod' ] : '';
if ( $secretmodule && file_exists( $moddir . $name . "/page.php" ) ) {
include_once( $moddir . $name . "/page.php" );
} else {
include_once( $moddir . "404.php" );
}
} else {
include_once( $moddir . "homepage.php" );
}
}
I am using htaccess to beautify the url as below.
RewriteRule ^([^/]*)/$ /index.php?mod=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?mod=$1&file=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/$ /index.php?mod=$1&file=$2&proid=$3 [L]
I want to add languages in session but i dont want to change the url of the main language (already indexed). only the other languages will be added an extra variable.
eg:
main language(de) : www.site.com/module/file/ /index.php?mod=$1&file=$2 [L]
other lang (en) : www.site.com/en/module/file/ /index.php?lang=$1mod=$2&file=$3 [L]
other lang (fr) : www.site.com/fr/module/file/ /index.php?lang=$1mod=$2&file=$3 [L]
I am just not sure how to setup the htaccess for these configurations.
You can have these set of rules in .htaccess:
RewriteEngine On
# ignore all files are directories from rules below
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# rules with any lang component
RewriteRule ^(en|fr)/([^/]*)/$ index.php?lang=$1&mod=$2 [L,QSA]
RewriteRule ^(en|fr)/([^/]*)/([^/]*)/$ index.php?lang=$1&mod=$2&file=$3 [L,QSA]
RewriteRule ^(en|fr)/([^/]*)/([^/]*)/([^/]*)/$ index.php?lang=$1&mod=$2&file=$3&proid=$4 [L,QSA]
# rules without any lang component
RewriteRule ^([^/]*)/$ index.php?lang=de&mod=$1 [L,QSA]
RewriteRule ^([^/]*)/([^/]*)/$ index.php?lang=de&mod=$1&file=$2 [L,QSA]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/$ index.php?lang=de&mod=$1&file=$2&proid=$3 [L,QSA]
With your shown attempts, please try following. Make sure your index.php file and htaccess both files are present in same folder(root) in your case.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rules for en OR fr languages URLs to be rewritten in backend.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(en|fr)/([^/]*)/([^/]*)/?$ /index.php?lang=$1mod=$2&file=$3 [QSA,NC,L]
##Rules for links which are not started with en OR fr links.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ /index.php?lang=$1mod=$2&file=$3 [QSA,NC,L]

CakePHP Images in webroot not displaying in Production

I have pushed my website to Production yesterday, all is working fine with the 3 .htaccess files I created. The only issue is that the images from my webroot folder are not displayed.
I am calling them with $this->Url->image('pages/' . $page->image)
Link rendered in html :
background-image:url(/app/img/pages/home.jpg);
'App' => [
'namespace' => 'App',
'encoding' => env('APP_ENCODING', 'UTF-8'),
'defaultLocale' => env('APP_DEFAULT_LOCALE', 'fr_FR'),
'defaultTimezone' => env('APP_DEFAULT_TIMEZONE', 'Europe/Brussels'),
'base' => false,
'dir' => 'src',
'webroot' => 'webroot',
'wwwRoot' => WWW_ROOT,
//'baseUrl' => env('SCRIPT_NAME'),
'fullBaseUrl' => false,
'imageBaseUrl' => 'img/',
'cssBaseUrl' => 'css/',
'jsBaseUrl' => 'js/',
'paths' => [
'plugins' => [ROOT . DS . 'plugins' . DS],
'templates' => [ROOT . DS . 'templates' . DS],
'locales' => [RESOURCES . 'locales' . DS],
],
],
/var/htdocs/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
/app/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
/app/webroot/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Thank you.
Here is my error Log
2021-10-21 13:18:34 Error: [Cake\Http\Exception\MissingControllerException] Controller class Img could not be found. in /homepages/20/d887310999/htdocs/app/vendor/cakephp/cakephp/src/Controller/ControllerFactory.php on line 226
Stack Trace:
/homepages/20/d887310999/htdocs/app/vendor/cakephp/cakephp/src/Controller/ControllerFactory.php:65
/homepages/20/d887310999/htdocs/app/vendor/cakephp/cakephp/src/Http/BaseApplication.php:311
Request URL: /img/pages/home.jpg

How to configure the .htaccess file for ErrorDocument working?

Good day! I'm doing the website on PHP. It will be a static site with limited count of pages. These pages have idential styles and scripts, so I wrote the viewpage.php script, which get the page name by URL and show it by PHP instruction include if such page is setted or put error 404 otherwise.
It is a structure of my website files:
admin/
auth.php
exit.php
index.php
pages/
index.php
contacts.php
...
err/
err404.php
.htaccess
viewpage.php
and part of viewpage.php
$pages = [
"index/" => [
"page" => "index.php"
],
"contacts/" => [
"page" => "contacts.php"
],
];
...
$requestPage = $_GET[ "page" ] . "/";
if ( !array_key_exists( $requestPage, $pages ) ) {
http_response_code( 404 );
include "pages/err/err404.php";
exit();
}
...
include "pages/$pageFile"; ?>
I want to allow the direct access to site/admin/[index|auth|exit].php and get such redirections:
site/admin/ => site/admin/index.php
site/ => site/viewpage.php?page=index
site/contacts => site/viewpage.php?page=contacts
site/contacts/ => site/viewpage.php?page=contacts
otherwise => site/pages/err/err404.php
I have tried this variant of .htaccess:
ErrorDocument 404 /pages/err/err404.php
RewriteEngine On
RewriteRule ^admin$ admin/index.php [L]
RewriteRule ^admin/$ admin/index.php [L]
RewriteRule ^([a-z\-]+)$ viewpage.php?page=$1 [L]
RewriteRule ^([a-z\-]+)/$ viewpage.php?page=$1 [L]
but I have problems: I can't open main page by site/, only by site/index/
How me to configure the .htaccess file?

how to create htacess to rewrite the route in yii2

I use yii2 to develop a website, and I use the Virtual host.
The question is in yii2 the source code is put in the web folder, and I have to visit the website like this "http://www.mydoname.com/web/index.php", if I want to visit like "http://www.mydoname.com/index.php" ,how to create the .htaccess in the root, what should I write?
Lets try this:
1- Create .htaccess file on the root directory(yii2-app) with this content
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} /(uploads)
RewriteRule ^uploads/(.*)$ uploads/$1 [L]
RewriteRule ^(.*)$ frontend/web/$1 [L]
</IfModule>
# Deny accessing below extensions
<Files ~ "(.json|.lock|.git)">
Order allow,deny
Deny from all
</Files>
# Deny accessing dot files
RewriteRule (^\.|/\.) - [F]
2- Create .htaccess file in frontend/web with the below contents
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
3- Now, open the frontend/config/main.php file and set $baseUrl for \yii\web\Request and \yii\web\urlManager.
<?php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
use \yii\web\Request;
$baseUrl = str_replace('/frontend/web', '', (new Request)->getBaseUrl());
return [
'id' => 'app-frontend',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'frontend\controllers',
'components' => [
'request' => [
'baseUrl' => $baseUrl,
],
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'urlManager' => [
'baseUrl' => $baseUrl,
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => []
]
],
'params' => $params,
];
Visit: localhost/yii2-app/
Original reference

How to access a controller with pretty url in Yii2 [duplicate]

Simple question. I use Yii2 advanced template. In apache I have DocumentRoot "{$path}/www/yii-application1/frontend/web". How can I access /www/yii-application1/uploads in order to show image to user?
Following code does not work:
<?php echo Html::img('../../uploads/ring.jpg') ?>
It works with DocumentRoot "{$path}/www/yii-application1/". But in this case an index page of website looks like domain.com/frontend/web. But I need just domain.com.
Step : 1
First create .htaccess file in here yii-application1/.htaccess
Options +FollowSymlinks
RewriteEngine On
# deal with backend first
RewriteCond %{REQUEST_URI} /(backend)
RewriteRule ^backend/assets/(.*)$ backend/web/assets/$1 [L]
RewriteRule ^backend/css/(.*)$ backend/web/css/$1 [L]
RewriteRule ^backend/image/(.*)$ backend/web/image/$1 [L]
RewriteCond %{REQUEST_URI} !/backend/web/(assets|css|image)/
RewriteCond %{REQUEST_URI} /(backend)
RewriteRule ^.*$ backend/web/index.php [L]
RewriteCond %{REQUEST_URI} /(assets|css|js|img|font)
RewriteRule ^assets/(.*)$ frontend/web/assets/$1 [L]
RewriteRule ^css/(.*)$ frontend/web/css/$1 [L]
RewriteRule ^js/(.*)$ frontend/web/js/$1 [L]
RewriteRule ^image/(.*)$ frontend/web/image/$1 [L]
RewriteCond %{REQUEST_URI} !/(frontend|backend)/web/(assets|css|js|image|font)/
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ frontend/web/index.php
Step : 2
Now create a components/Request.php file in common directory and write below code in this file.
Request.php file
<?php
namespace common\components;
class Request extends \yii\web\Request
{
public $web;
public $adminUrl;
public function getBaseUrl(){
return str_replace($this->web, "", parent::getBaseUrl()) . $this->adminUrl;
}
public function resolvePathInfo(){
if($this->getUrl() === $this->adminUrl){
return "";
}else{
return parent::resolvePathInfo();
}
}
}
?>
Step : 3
Now Installing component. Write below code in frontend/config/main.php and backend/config/main.php files respectively.
//Frontend
'components' => [
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'identityCookie' => [
'name' => '_frontendUser', // unique for frontend
]
],
'session' => [
'name' => 'PHPFRONTSESSID',
'savePath' => sys_get_temp_dir(),
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<alias:>' => 'site/<alias>',
],
],
'request'=>[
'cookieValidationKey' => '[gfhjghsdjks44fdf4fgf4fgfg5645ggxcvvc]',
'csrfParam' => '_frontendCSRF',
'class' => 'common\components\Request',
'web'=> '/frontend/web'
],
]
//Backend
'components' => [
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => false,
'identityCookie' => [
'name' => '_backendUser', // unique for backend
]
],
'session' => [
'name' => 'PHPBACKSESSID',
'savePath' => sys_get_temp_dir(),
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
'request'=>[
'cookieValidationKey' => '[ruiqwybnddiogj786789hzcassdas9dasdjufi]',
'csrfParam' => '_backendCSRF',
'class' => 'common\components\Request',
'web'=> '/backend/web',
'adminUrl' => '/backend'
],
]
Your domain.com/frontend/web problem solved to follow the above steps.
and you can access the index page for domain.com/frontend/web using domain.com.
Now You can access you image using
<?php echo Html::img('uploads/ring.jpg') ?>
Also you can access the domain.com/frontend/web/image using below code
<?= Html::img(Yii::getAlias('#web').'/image/xyz.png', ['class' => 'retina']); ?>
also you get webroot path using this Yii::getAlias('#webroot')
You can also use alise inside config array on web.php file of config folder like
'aliases' => [
'#uploads' => '#app/web/upload',
],
and use it in any where you want like
<?= Html::img("#uploads/image/xyz.png", ['class' => 'retina']); ?>

Resources