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.
Related
am trying to create a url like this using htaccess...
www.example.com/user/david
what i have now is this www.example.com/user?username=david
It works well when i do something like
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*/([a-zA-Z0-9_-]+)|([a-zA-Z0-9_-]+))$ ./user.php?username=$1 [L]
But i am wrong at my php code.
if(isset($_GET["username"]))
{
$stmt = $mysqli->prepare("SELECT * FROM campus_users WHERE campus_name = ?");
$stmt->bind_param("s", $campus_name );
$campus_name = trim($_GET["username"]);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows == 1) {
$row = $result->fetch_array(MYSQLI_ASSOC);
//displaying users rows
} else {
header("location: blank-page");
exit();
}
} else {
header("location: home.php");
exit();
}
But when i enter the url like this www.example.com/user?username=david it works well...
The problem is i want a smart looking url which isnt working due to my php i guess.
Please help me.
Thanks in advance.
If your URL with query string is working fine then it most probably it means your .htaccess rules are not working, usually we give user friendly URLs to users in your case it should be www.example.com/user/david and internally it should route to www.example.com/user.php?username=david. Please try following Rules in your .htaccess file.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/user/(.*)$
RewriteRule ^.*$ /user.php?username=%1 [QSA,NC,NE,L]
NOTE: if possible and you are not on prod systems then you could try to restart your Apache service too, though it's not necessary but sometimes it may be required. Also use user friendly URL as mentioned above in browser make sure you clear the browser cache before hitting it.
Hi I am trying to change my url using htaccess but it didn't work anymore.
http://localhost:8888/cPanel/abc?page=general-settings
RewriteRule ^cPanel/([\w-]+)/?$ abc.php?page=$1 [L,QSA]
What i am doing wrong anyone can help me here please ?
I want to change the url like this:
http://localhost:8888/cPanel/general-settings
<?php
$page ='';
if($_GET['page']){
$page = $_GET['page'];
if($page == 'general-settings'){
include "/pages/general-settings.php" ;
}
}
?>
The error stands in the rule. Your actual rule is:
RewriteRule ^cPanel/([\w-]+)/?$ abc.php?page=$1 [L,QSA]
which is missing cPanel, if you want to achieve http://localhost:8888/cPanel/general-settings as result
With this rule (which means: when you Apache match cPanel/*anything*, hit the resource at cPanel/abc.php?page=*anything*) it should work:
RewriteRule ^cPanel/(.*)$ cPanel/abc.php?page=$1 [L,QSA]
Test this code
RewriteEngine on
Options +FollowSymlinks
RewriteRule ^cPanel/(.+)/?$ abc.php?page=$1
url: http://localhost:8888/cPanel/general-settings
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.
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...
I want:
sub.domain.com to load index.php
sub.domain.com?s=custom to load /custom/index.php
I'm doing this right now:
In .htaccess:
RewriteRule ^(custom)/?$ index.php?subject=$1
and in index.php I have this:
if($_GET['s'] == 'custom') {
header( 'Location: http://sub.domain.com/custom/index.php' ) ;
}
... but is it possible to do the redirect via htaccess itself depending on the GET variable?
Thanks!
Yes, you could check query string with RewriteCond
RewriteCond %{QUERY_STRING} s=custom
RewriteRule ^(.*?)$ custom/index.php [L]
With this, it redirects all requests with existing "custom" get parameter to index.php, and this can be extended :)
Idea: http://statichtml.com/2010/mod-rewrite-baseon-on-query-string.html
You probably want to use "RedirectMatch"
RedirectMatch ^sub.domain.com?s=custom$ /custom/index.php
Maybe youll need to play with the regex abit