Our site allows for a pretty url 'etest.me/1234' where '1234' is a client id. As you can see below it redirects to our route.php file where we do our redirect. The problem is when a client uses 'etest.me' without the '/1234' they get the apache 'The requested URL /go/ was not found on this server.' message. I would like the url to go to another page when the '/1234' is missing.
Note that we have the domain and path forwarded to the non-existing '/go' directory so the rules below will catch it. The following is in our .htaccess file in the root directory.
RewriteEngine On
#restrict rewriting URLs ONLY to paths that DO NOT exist
RewriteCond %{SCRIPT_FILENAME} !-d
# commented out to speed up looking since we are not processig file names anyway
#RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^go/([a-zA-Z0-9/]+)$ ./route\.php?go=$1
Working perfectly fine for me. Hope this will work for you as well.
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^go/([a-zA-Z0-9]+)$ /route.php?go=$1 //when client id exist
RewriteRule ^([a-zA-Z0-9]+)$ /index.php // when no client id
You can add a new rule to handle this situation:
RewriteRule ^go/$ ./another_script\.php
I accepted the answer by Sahil and posted the entire logic below for others to use:
Domain: etest.me
Pretty URL: etest.me/1234 (where '1234' is a client id)
Forwarded to: etesting.io/go (with forward path option selected so the '/1234' will be include in the forward)
Note: There is no '/go' directory on the server, '/go' is used to trigger the redirect.
Wanted 'etest.me' (without client id) to go to index file in root directory.
Here is the rewrite rules in .htaccess:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^go/([a-zA-Z0-9/]*)$ ./route\.php?go=$1
Here is the route.php code:
<?php
if($_GET['go']){
$a = explode('/', $_GET['go']);
$c = count($a);
if($c == 1 || $c == 2){
header('location: ../../index.php?aid=' . $a[0] . '&tid=' . $a[1]);
exit;
}
else{
die('Invalid URL');
}
}
else{
header('location: ../index.php');
exit;
}
And finally the index.php file:
if($_GET['aid'] && $_GET['tid']){
header('location: test/register.php?aid=' . $_GET['aid'] . '&tid=' . $_GET['tid']);
exit;
}
elseif($_GET['aid']){
header('location: test/index.php?aid=' . $_GET['aid']);
exit;
}
else{
header('location: account/index.php?' . $_SERVER['QUERY_STRING']);
exit;
}
Hope this helps somebody in the future.
I would like a more elegant solution but it's behind me for now...
Related
I'm trying to figure out how to convert url. It leads to the index page.
the constant I define
define("URL_PAGE", "page.php?p=");
link:
<a class="menu-link" href="'.BASE_URL.URL_PAGE.$row1['page_slug'].'">';
.htaccess
RewriteRule ^page/(.*)$/?$ page.php?p=$1 [NC,L]
result:
http://localhost/aione/page.php?p=about-us
I'm trying to catch the incoming link like this.
if(!isset($_REQUEST['p']))
{
header('location: index.php');
exit;
}
else
{
// Check the page slug is valid or not.
$statement = $pdo->prepare("SELECT * FROM mbo_page WHERE page_slug=? AND status=?");
$statement->execute(array($_REQUEST['p'],'Active'));
$total = $statement->rowCount();
if( $total == 0 )
{
header('location: index.php');
exit;
}
}
How should I write the rule so that it looks like this? Your help is appreciated.
http://localhost/aione/page/about-us
This seems to be a trivial question.
Try this:
<a class="menu-link" href="'.BASE_URL.'page/'.$row1['page_slug'].'">';
Or, you can try this instead:
define("URL_PAGE", "page/");
Your .htaccess file needs to look something like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/*page/([^/]*)$ page.php?id=$1 [L,QSA] #If static R=301
</IfModule>
Place .htaccess in the same directory (i.e. folder) as page.php. (e.g. in http://localhost/aione/)
Notes:
^/*page/([^/]*)$ page.php?id=$1 only works with the following style: "http://localhost/aione/page/about-us", if you want something like about-us/test to also be passed as page.php?id=about-us/test, use the following rule:
^/*page/(.*)$ page.php?id=$1 also matches for something like "http://localhost/aione/page/about-us/test/link", and it will rewrite to: page.php?id=about-us/test/link, meaning the following:
$_GET['id'] === 'about-us/test/link'; // true
as always, remember to validate and sanitize any user input.
After changing from HTTP to HTTPS in my OpenCart installation with Journal2 theme/plugin I have problem - admin panel for Journal2 not loads, showing "Loading..." state and that's all.
Journal2 version = 2.13
OpenCart version = 2.3.0.2
Current stack: PHP 7 + Apache2 + MySQL
Also adding screenshot.
What is broken? I have updated website url from http:// to https:// in admin panel, also updated variables in
config.php
and
/admin/config.php
files. Maybe problems with caching?
Kindly check the two config files for correct configuration of Data Base and file folders.
Also u need to check the file in system/library/url.php
on that,
public function link($route, $args = '', $secure = false) {
if ($this->ssl && $secure) {
$url = 'https://' . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['SCRIPT_NAME']), '/.\\') . '/index.php?route=' . $route;
} else {
$url = 'http://' . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['SCRIPT_NAME']), '/.\\') . '/index.php?route=' . $route;
}
if ($args) {
if (is_array($args)) {
$url .= '&' . http_build_query($args);
} else {
$url .= str_replace('&', '&', '&' . ltrim($args, '&'));
}
}
foreach ($this->rewrite as $rewrite) {
$url = $rewrite->rewrite($url);
}
return $url;
}
Hope It will helps you.
Did you properly configure your site for SSL use? Let's go over some things here:
Not to sound repetitive, but are you very sure that both your config files are correct? Not sure what you meant before when you said you took "the path for the root" from some page when it's pretty much the url that anyone's going to go visit your site/store. I'm assuming your store/opencart is in it's own folder and not in the root directory, yes? If so:
admin/config.php file should have something like this:
// HTTP
define('HTTP_SERVER', 'http://www.yourwebsite.com/opencart/admin/');
define('HTTP_CATALOG', 'http://www.yourwebsite.com/opencart/');
// HTTPS
define('HTTPS_SERVER', 'https://www.yourwebsite.com/opencart/admin/');
define('HTTPS_CATALOG', 'https://www.yourwebsite.com/opencart/');
config.php file should have something like this:
// HTTP
define('HTTP_SERVER', 'http://www.yourwebsite.com/opencart/');
// HTTPS
define('HTTPS_SERVER', 'https://www.yourwebsite.com/opencart/');
and by what I recall from past experience that forward slash matters at the end.
Also, it matters very much if your url is: yourwebsite.com vs www.yourwebsite.com just to point out that, too.
Speaking of which... how about your .htaccess file? That could be a culprit here, too. I'm referring to the htaccess file for your root directory and not opencart's htaccess. That may need to be altered correctly to for https. As an example:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourwebsite.com/$1 [R,L]
RewriteCond %{HTTP_HOST} ^(www.)?yourwebsite.com$
RewriteCond %{REQUEST_URI} !^/opencart/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /opencart/$1
RewriteCond %{HTTP_HOST} ^(www.)?yourwebsite.com$
RewriteRule ^(/)?$ opencart/index.php [L]
If you notice here, you need to include the server port and https. So maybe check that, too.
I am running a site, where I am trying my own slug mechanism.
here are my .htaccess file contents
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L,QSA]
</IfModule>
and in my index.php I have following code
$_REQUEST['action'] = trim($_SERVER['REQUEST_URI'],"/");
...
if (!empty( $_REQUEST['action'] )) {
$action = explode("/", $_REQUEST['action']);
$slug = $action[0];
$pageName = $slug.".php";
if (file_exists($pageName)) {
require_once $pageName; // 404 status code
}
else {
echo 'Your page is not found'; // 200 status code
}
} else {
require_once 'welcome.php';
}
now how it will work is
/school will be mapped on school.php
/mobile will be mapped on mobile.php
/about will be mapped on about.php
apparently this work perfectly fine, contents are loaded fine, but issue with HTTP status code which is 404
on the other hand if I type some thing random like
/some-thing-random it shows this line echo 'Your page is not found'; with 200 status code (please note some-thing-random.php does not exist)
What strangely worked for me is as follows, I just added following line in .htaccess file and its working fine now with 200 message
Options +FollowSymLinks -MultiViews
for all those concerned about manually setting header: I had not. and it should not, all sort of urls will be intercepted by index.php, at least this is what my understaing is about my .htaccess file
one more strange thing is that might help you to respond to my question is
let say
if url is as follows
www.mysite.com/about and if rename file about.php as xabout.php and then just perform following
$fileName = 'x'.$slug.'.php';
require_once($fileName);
it works fine
Now my question is what on earth is happning? can any one please explain this to me (also note that I am using GoDaddy hosting)
To send a 404 code, it must be indicated in the header.
header("HTTP/1.0 404 Not Found");
More infos on http://php.net/manual/fr/function.header.php
I have this specific problem where I have to check URL if its part is 8 chars long hash code that is saved in my database or its just normal URL where you want to navigate.
For example if i write url :
- www.example.com/A4s8Gga3
i want to process it with my script in php file
And if i write url :
-www.example.com/my-books
-www.example.com/about
i want to navigate on those pages.
I know i have to use htaccess (so much I managed myself) and so far it looks like this :
#part1
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} (\/\w+)$ [NC]
RewriteRule ^(.*)$ mobile_redirect.php [L]
#part2
RewriteCond %{REQUEST_URI} (/|\.htm|\.php|\.html|/[^.]*)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php
My mobile_redirect.php looks like this:
ob_start();
require_once('connect.php');
$request = $_SERVER['REQUEST_URI'];
$request_hotovy = str_replace('/', '', $request);
$request_hotovy = mysql_real_escape_string($request_hotovy);
$select = "SELECT HASH_ID,OFFER FROM kasko_send_form WHERE MOBILE_HASH_ID = '".$request_hotovy."'";
$query = mysql_query($select);
if(mysql_num_rows($query) > 0){
// request is mobile hash id
$result = mysql_fetch_array($query);
$hash_id = $result['HASH_ID'];
header("Location: some_link?def=".$hash_id);
} else {
// request is normal url
header("Location: ".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
}
I know that it will end up redirecting in loop. I tried to put part1 after part2 and still have the same problem. I am using joomla and it have many urls (which im not able to write down) that are not real directories or files that is why i cant just use in my php file this solution :
ob_start();
require_once('connect.php');
$request = $_SERVER['REQUEST_URI'];
$request_hotovy = str_replace('/', '', $request);
$request_hotovy = mysql_real_escape_string($request_hotovy);
$select = "SELECT HASH_ID,OFFER FROM kasko_send_form WHERE MOBILE_HASH_ID = '".$request_hotovy."'";
$query = mysql_query($select);
if(mysql_num_rows($query) > 0){
// request is mobile hash id
$result = mysql_fetch_array($query);
$hash_id = $result['HASH_ID'];
header("Location: some_link?def=".$hash_id);
} else {
// request is normal url
header("Location: page_not_found.php");
}
Because there is clearly more url processing done in joomla after it ends reading my htaccess (i dont know much about joomla either).
Can you guys give me a hint how to process the url (then maybe alter it so it wont end up in loop and then alter it again after the part1 back to normal so it can continue processing as it would normally)?
Also if you guys have any good tutorials where I could learn such things it would be really helpfull, because i understand only basics of regex and how htaccess works ...
If you use Joomla for most of your URLs exact the one that should have this eight character string there is a simple solution for this.
Just use the regular Joomla .htaccess file and add two lines before RewriteBase /
RewriteCond %{REQUEST_URI} ^/[A-Za-z0-9]{8}$
RewriteRule .* mobile_redirect.php [L]
But the Problem here is that if you have regular URLs in Joomla with 8 character than they would be redirected es well e.g. http://example.com/lastnews
So for this URL's you have to add a exception, and the hole thing would lock like this:
RewriteCond %{REQUEST_URI} !^/latesnews$ [NC]
RewriteCond %{REQUEST_URI} !^/youandme$ [NC]
RewriteCond %{REQUEST_URI} ^/[A-Za-z0-9]{8}$
RewriteRule .* mobile_redirectt.php [L]
There is no way to redirect back to Joomla with the same URL if your script do not find a record in your DB. Either your script is handling the URL or Joomla dose it. So you have to provide a 404, or find a way to include the index.php file from Joomla in your script.
I've just realized my host URL are case-sensitive.
It means that /homepage, /Homepage and /homePage are different URLs, which is problematic.
I could force any URL to be lower-case and it would fix my problem but this is not exactly what I'm looking for.
I would like to know if I can redirect any variant of /homepage (ie:/hOMEpaGe,/HOMEPAGE,/hOmEpAgE,...) to /homePage (Note the case-sensivity)
I don't know if I'd better handle it with the server configuration or into the php file by manipulating the $_SERVER[REQUEST_URI].
Thank you for any idea on this !
Sure it might be possible to do a redirect on the Bootstrap file (index.php for example) with the combination of strtolower, stristr(), str_replace() and header() Functions like so:
<?php
// ASSUMING THE BASE URI IS KNOWN
$baseURI = "http://www.my-domain.com";
// CONVERT ALL CHARACTERS IN THE $_SERVER['REQUEST_URI'] TO LOWER-CASES
$uri = strtolower($_SERVER['REQUEST_URI']);
$rdURI = str_replace("homepage", "homePage", $uri);
// USE EITHER A SWITCH OR IF CONDITIONAL LOGIC TO SET THE URI
if(stristr($uri, "homepage")){ // REDIRECT ANY THING WITH HOMEPAGE TO: homePage
$rdURI = str_replace("homepage", "homePage", $uri);
}else if(stristr($uri, "another-uri-1")){
$rdURI = str_replace("another-uri-1", "another-URI-1", $uri);
}else if(stristr($uri, "another-uri-2")){
$rdURI = str_replace("another-uri-2", "another-URI-2", $uri);
}else if(stristr($uri, "yet-another-uri")){
$rdURI = str_replace("yet-another-uri", "yet-Another-URI", $uri);
}else{
// DEFAULT TO THE HOMEPAGE IF ALL ELSE FAIL
// THIS DECISION IS UP TO YOU AS YOU MIGHT NOT NEED IT TO WORK THIS WAY...
$rdURI = str_replace("homepage", "homePage", $uri);
}
// REDIRECT TO THE NEW URI...
header("location: " . $rdURI);
exit;
// OR REDIRECT TO THE NEW URI WITH BASE URI...
header("location: " . $baseURI . DIRECTORY_SEPARATOR . $rdURI);
exit;
It is always a better idea to handle this on a lower level than having redirects in PHP.
If order to achieve that, first you need to declare a RewriteMap
# Add RewriteMap for redirecting to lowercase URIs
<IfModule mod_rewrite.c>
RewriteMap lc int:tolower
</IfModule>
Then just create the RewriteRule to redirect uppercase URLs to lowercase ones.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^(.*)$ ${lc:$1} [R=301,L]
Use .htaccess [NC] for case insensitive:
RewriteCond %{REQUEST_URI} /phpMyAdmin [NC]
lmgtfy: https://perishablepress.com/case-insensitive-redirectmatch/