a test site using htaccess help needed - .htaccess

I have my website at root website .com/ I want to setup a folder like this website .com/us/
The folder /us/ is empty and when someone goes to it the folder shows the same content as what's in website .com but still staying on website .com/us/.
Here's the twist, if i wanted to show a different header.php or js or css file and this is loaded in to the domain website .com/us/, i need htaccess to use that instead of what's on website .com/. I will use the same directory structure.

Here's one of many ways you can go about it. It's a crude example using mod_rewrite and php5 that hopefully gets you in the direction you want to go.
Using the example below navigating to should behave like the following
http://example.com/ should show 'Content' with the header-default.php header
http://example.com/us/ should show 'Content' with the header-us.php header
http://example.com/some-other-directory/file.php should show 'Different Content in some other directory' with the header-default.php header
http://example.com/us/some-other-directory/file.php should show 'Different Content in some other directory' with the header-us.php header
.htaccess
<IfModule php5_module>
php_value auto_prepend_file /var/www/vhosts/example.com/httpdocs/header.php
php_value auto_append_file /var/www/vhosts/example.com/httpdocs/footer.php
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^us/?$ /index\.php?tpl=us [QSA,L]
RewriteCond %{DOCUMENT_ROOT}/$1 !-f
RewriteRule ^us/(.*)$ /$1?tpl=us [QSA,L]
</IfModule>
/header.php
<?php
$tpl = $_GET['tpl'];
if ($tpl === 'us') {
require_once $_SERVER['DOCUMENT_ROOT'].'/header-us.php';
}
else {
require_once $_SERVER['DOCUMENT_ROOT'].'/header-default.php';
}
?>
/header-us.php
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>USA</title>
<meta name="description" content="Welcome to the US section">
<link rel="stylesheet" href="/css/us/style.css">
<script src="/js/us/scripts.js"></script>
</head>
<body>
/header-default.php
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to index</title>
<meta name="description" content="Welcome to the DEFAULT section">
<link rel="stylesheet" href="/css/style.css">
<script src="/js/scripts.js"></script>
</head>
<body>
/footer.php
<p>user contributions licensed under cc-wiki with attribution required</p>
</body>
</html>
/index.php
Content
/some-other-directory/file.php
Different Content in some other directory

Related

Angular production build with index.jsp instead of index.html

I want want to create an index.jsp file instead of index.html while doing production build. This weird requirement is for capturing the header part which we will getting from other Oracle Authentication Manager so I want load my appliation from index.jsp. My index.jsp should look something like this
<%# page language="java" import="java.util.*" session="true" %>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CardEncryption</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<link rel="stylesheet" href="styles.3ff695c00d717f2d2a11.css"></head>
<body>
<app-root></app-root>
<script type="text/javascript" src="runtime.06daa30a2963fa413676.js"></script><script type="text/javascript" src="polyfills.9a8743e2009b1e7b1fbd.js"></script><script type="text/javascript" src="main.dcf4ad6dff6130812df3.js"></script></body>
</html>
Any suggestion to do this.
I suppose you only want the index.jsp when doing the production build. The development process should still use index.html
In that case you should
Create a index.jsp file in the same directory as your index.html file (so you have both).
Update your angular.json file's production configuration by adding an index property to your production configuration like this:
That will make sure that it uses index.jsp when you build with --prod, but still uses index.html for development.

why slim requires full directory path for loading external assets(js/css) file

<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script type="text/javascript" src="/slim/app/views/animations/jquery.min.js"></script>
<script type="text/javascript" src="/slim/app/views/animations/bootstrap.min.js"></script>
<link rel="stylesheet" href="/slim/app/views/animations/bootstrap.min.css">
</head>
<body>
<div class="container">
hello
</div>
</body>
</html>
I have to specify full directory path for loading the js and css files
(jquery.min.js and bootstrp.min.js and bootstrap.min.css).Here i have
(/slim/app/views/animations/) as the directory which contain all the
files. if currently i am in the views directory i should use the
(animations/whatever the file name) but it is not working in these
way. but if i use the full directory path it all works fine. why these
is happening can anyone explain.
(slim) is my root directory where i installed slim
Using Relative paths is not how you typically work with Slim. Your assets (JavaScript/CSS/images, etc) should be referenced absolute from your views:
src="/assets/js/myapp.js"
Routed URLs do not map directly to file system based resources because templates should not be accessed by URL publically - they are only served by the controllers.
A common slim file structure looks like this:
app/
public/index.php
templates/
view.html
assets/
images/
js/
css/
Some add the assets subfolders directly into the public folder.
Others have those folders on the root level.
However, in general, public assets (e.g. CSS, JS, images) should be beneath the public document root (accessible to the public).
One thing that could be helpful if you do not like this behavior is to use Slim's basePath variable.
You should be able to set the base URL as a View variable in a slim.before callback in your index.php so that it is available to all routes like this:
$app->hook('slim.before', function () use ($app) {
$app->view()->appendData(array('baseUrl' => '/base/url/here'));
});
or
$app->hook('slim.before', function () use ($app) {
$posIndex = strpos( $_SERVER['PHP_SELF'], '/index.php');
$baseUrl = substr( $_SERVER['PHP_SELF'], 0, $posIndex);
$app->view()->appendData(array('baseUrl' => $baseUrl ));
});
And then apply it to the references in your HTML tag of the base template file and that's it.
<link rel="stylesheet" type="text/css" href="{{ base_url() }}/css/style.css" />
Or use Uri::getBaseUrl(), e.g.
$basePath = $request->getUri()->getBasePath();

Show the link as if it was linked to the file using htaccess

can we show the url like this using htaccess
http://www.example.com/group/goroupName
I have group.php file in the pages folder. I want to show the url like above.
I have tryed it like this rule
RewriteRule ^group/([\w-]+)/?$ pages/group.php?group_username=$1 [L,QSA]
but some included php files not displayed. For example :
<?php
include_once '../../functions/includes.php';
if(isset($_GET['group_username'])) {
$group_username=$_GET['group_username'];
include_once '../../functions/includes/get_group.php';
if(empty($group_profile_owner_id)){
header("Location:$url404");
}
}else{
header("Location:$url404");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="theme-color" content="#8e24aa">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
<title><?php echo $group_username;?></title>
<?php include("../../themes/$getTemplate/contents/style_js.php");?>
</head>
<body>
in the above code this include not displayed:
<?php include("../../themes/$getTemplate/contents/style_js.php");?>
What is the problem in my RewriteRule
Your RewriteRule is correct and should work.
Problem is with include function in PHP where you're using a relative path. Due to URL rewrite this relative path changes.
You are better off using a relative path from DOCUMENT_ROOT as:
<?php
include($_SERVER['DOCUMENT_ROOT'] . '/themes/themeName/contents/style_js.php');
?>

Solving shortening url with htaccess

I have rewritten my url using htaccess .After navigating to the page, the layout distorts and no value fetched from the database is printed out.This is the main URL
http://www.deffsale.com/personalBusiness/PersonalBusiness.php?item=ladys%20Fashion&page=1
and I'm navigating to this page using this one
http://www.deffsale.com/personalBusiness/all/ladys-Fashion/1
This is my ht-access
RewriteEngine On
RewriteRule ^About-us AboutUs.php [NC,L]
RewriteRule ^personal-business personalBusiness [NC,L]
RewriteRule ^personalBusiness/all/([0-9a-zA-Z_-]+)/([0-9]+) personalBusiness/PersonalBusiness.php?item=$1&page=$2 [NC,L]
please help me solve this because it has been a disturbance past three months
You need to include this line just below <head> section of your HTML:
<base href="/personalBusiness/" />
so that every relative URL is resolved from that base URL /personalBusiness/ and not from the current page's URL.
It is a path issue with your CSS and images related to the rewrite rules. Add a rule like this before the other rules:
RewriteRule \.(js|css|jpe?g|png|gif)$ - [L]
which will allow those requests to be passed unmodified.
In addition, make sure the URLs for JavaScript, CSS and image files are coded relative to DocumentRoot. For example if your files are organized like this:
/ - index.php
+ css
+ base.css
+ color.css
+ images
+ logo.png
The style tags would be:
<link rel="stylesheet" type="text/css" href="/css/base.css">
<link rel="stylesheet" type="text/css" href="/css/color.css">
And images would be:
<img src="/images/logo.png" alt="logo">
As the other respondent suggested, you may use a base tag with relative URLs instead. In that case, you would code the tags like:
<base href="/">
<link rel="stylesheet" type="text/css" href="css/base.css">
<link rel="stylesheet" type="text/css" href="css/color.css">
And images would be:
<img src="images/logo.png" alt="logo">
The difference is that the base tag can be used to set the reference point for the relative URLs. Bear in mind the base tag will affect all relative URLs on your page.

mod_rewrite .htaccess - Stylesheets mess up

I'm trying to rewrite some URL's on my website, and it works fine, I just have some issues with the stylesheets. All stylesheets works at index.php but not when url's like: localhost:8888/folder/path/(1,2,3,4)
I have my stylesheets in a file where they're listed like this:
<!-- CSS -->
<link rel="stylesheet" type="text/css" href="includes/css/site.css"/>
<link rel="stylesheet" href="includes/css/viewProfile.css" type="text/css" media="screen" title="no title" charset="utf-8">
<link rel="stylesheet" href="includes/css/viewProvider.css" type="text/css" media="screen" title="no title" charset="utf-8">
Is there a way to fix this with mod_rewrite or how would I fix this?
This is because you use a relative path to the style sheets. Note that the browser sends requests to
localhost:8888/folder/path/includes/css/...
in that case. If the styles always reside in that configuration at the server root, you could simply make the relative URI an absolute one. Or you remove the path portion for CSS files on the server side.

Resources