I'm trying to hide the php page name in url which redirects after login by header function. So far I can hide the index file but can't hide the file which redirects after login. Here are my codes,
PHP script for after login events
$sql_login = mysql_query("SELECT * FROM sms_people WHERE username='$username' AND password='$password'");
$row = mysql_fetch_array($sql_login);
if ($row > 0 && $row[5] == 1) {
header('Location: adminpanel.php');
}
.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
How can I hide the files which redirects from header function by using .htaccess file? Need this help badly. Tnx.
there is an alternative way to redirect php files and hide them
for example
http://yourwebsite.com/user/login/
user part main user.php or you can change the file name
firstl, in your htaccess redirect to index php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
############### SEO ##########################
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
in your index.php file
$rootfolder="parts"
if(isset($_GET["url"])){
$part= explode("/", $_GET["url"]);
if(isset($part[0]))
$controller = ''.strtolower($part[0]).'';
if(isset($parca[1]))
$method = ''.strtolower($part[1]).'';
if(isset($parca[2]))
$id = ''.$part[2].'';
if( file_exists($rootfolder."/".$controller.php))
{
//you can call the file if it is exits
requre_once $rootfolder."/".$controller.php;
}else{
die("404 not found !");
}
}
Ok got a solution. I changed the information in header function & placed the file corresponding to the given information in .htaccess file. Here are the codes,
PHP Script
if ($row > 0 && $row[5] == 1) {
header('Location: AdminPanel'); // Instead of adminpanel.php
}
.htaccess
RewriteRule ^AdminPanel/?$ adminpanel.php [R,NC,L] // Added this line.
Tnx for all your efforts btw.
Related
favorite
I am building a website and I would like to customize the users' profile so their profile url shares their name. For example, the website domain would be www.example.com and the users' url would be www.example.com/username.
at present when i do a test sign it looks like test/admin/index.php
i want to rewrite it as test/admin/testuser
I am assuming this is a convention because I see this all around the web. Is this done by giving each user their own directory and how painstaking would that be?
Usually this is done by routing. You redirect all requests to the index.php file with .htaccess
Your setup could look like this:
.htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?param=$1 [QSA,L]
index.php
<?php
if(empty($_GET['param'])) {
// code for frontpage
} else {
$username = $_GET['param'];
}
?>
In your htaccess you can try this:
RewriteRule ^/([a-zA-Z0-9_-]+)$ index.php?user=$1 [QSA,L]
And in your php:
if(isset($_GET['user'])) {
// code for frontpage
} else {
$username = $_GET['user'];
}
I hope this help you
my current url : http://example.com/blog-single.php?id=15
this is a dynamic blog page the "id" has to be change accordingly to blog adding
I need to change this url to http://example.com/blog-single/<coresponding-blog-name>
eg: http://example.com/blog-single/my-new-blog
how to Remove .php and id from the current URL?
First of you need to create a .htaccess file and add this code in it
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ blog-single.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ blog-single.php?id=$1
After that in your main file for example in index.php file you need to add the code
<?php
$key=$_GET['key'];
if($key=='my-new-blog')
{
include('my_new_blog.php'); // Home page
}
else if($key=='login')
{
include('login.php'); // Login page
}
else if($key=='terms')
{
include('terms.php'); // Terms page
}
else
{
include('users.php'); // Users Gateway
}
?>
try this according to your need.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/([a-zA-Z0-9_-]+)$ $1.php?id=$2 [NC,L]
here you can use your filename in manner http://example.com/blog-single/my-new-blog this will work for every page as per your requirement.
For example, if you access to this: http://example.com/blog/1234.html and real path is http://example.com/blog.php?id=1234 you can put this on your htaccess file:
RewriteEngine On
RewriteRule ^blog/([^/]*)\.html$ /blog.php?id=$1 [L]
This tool will help you: http://www.generateit.net/mod-rewrite/index.php
I'm working on a URL shortener script.
My script generates a link like http://127.0.0.1:1337/urlshortener/v5tjp.
v5tjp is a random value, generated by a script.
My script's logic is that I input an URL, then PHP takes it, generates a random value (with the length taken also from the SQL database), then inserts the long url and the short url in the database.
Where I'm stuck: I need to create a .htaccess file to redirect the visitor to redirect.php, where I have the redirect script.
This is the redirect.php file:
<?php
include ('connect.php');
$decode = mysql_real_escape_string($_GET['decode']);
$sql = 'SELECT * FROM urls WHERE short_code="$decode"';
$result = mysql_query($sql);
if (isset($_GET['url_token'])){
$urlId=$_GET['url_token'];
$query = "SELECT * FROM urls WHERE short_code=".$urlId." LIMIT 1";
$redirect = mysql_query($query);
if(mysql_num_rows($redirect)) {
$row = mysql_fetch_assoc($redirect);
$url = $row['long_url'];
header('Location: http://'.$url);
}
echo 'Bad URL!';
exit();
}
while($row = mysql_fetch_array($result))
{
$res=$row['long_url'];
header("location:".$res);
}
This is the .htaccess file I've made:
RewriteEngine On
RewriteRle ^$ index.php [L]
RewriteCond %(REQUEST_FILENAME) !-f
RewriteRule ^(.*)$ redirect.php?url_token=$1 [L]
But for some reason it's not working. I'm running my script with XAMPP.
RewriteRule ^$ index.php [L]
You missed a 'u'.
Try this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteRle ^$ /index.php [L]
RewriteCond %(REQUEST_FILENAME) !-f
RewriteRule ^urlshortener/(.*)$ /redirect.php?url_token=$1 [L,QSA,NC]
I use the following code to request the URI in order to display multi-language content variables. That works fine for every page that has an URI. My problem is the frontpage: For the pure domain www.domain.com without URI it doesn't work, however it works for www.domain.com/index.php. I can't figure out the right syntax for the frontpage, please help.
if (stripos($_SERVER['REQUEST_URI'],'index') !== false ) {
// Frontpage
$lang['META_TITLE'] = 'Welcome to the frontpage';
$lang['META_DESCRIPTION'] = '';
$lang['META_KEYORDS'] = '';
$lang['PAGE_TITLE'] = '';
$lang['BREADCRUMB'] = '';
}
Create a .htaccess file in main root and paste this code .... i hope its work for you
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
I want to redirect all to one script e.g. index.php?url=inputurl
With if/else I want to parse url
in index.php run query for url in my custom table
if url is mach: echo "ok"
else do nothing
How should I set .htaccess in root folder of Wordpress?
Example:
URLs in custom_table:
asd
dfg
ghj
If user puts:
www.mysite.com/asd
-> mod_rewrite should output this: www.mysite.com/index.php?url=asd
Else if user puts:
www.mysite.com/zzz
-> do nothing
I think the following .htaccess should solve the problem:
RewriteEngine On
# Redirects everything that is not index.php to index.php
RewriteCond $1 !^index\.php
RewriteRule ^(.*)$ index.php?url=$1 [L,R]
Edit: to not include your folders and files (like /js, /css, etc.) in rewrite, add the following lines before the RewriteRule line (see comments):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
And in the PHP script:
$url = $_GET['url'];
// the method is_valid should check if the page exists in DB
if (is_valid($url)) {
// do something here
// maybe redirect with header('Location: path')
} else {
// show a not found page (error 404)
}
You want to both be able to read from a database and do nothing if there is not match.
This would require you run code to access db then return back to apache to process and is not possible from .htacccess (though it is from httpd.conf).
The .htaccess solution would be to specify all the "table" entries inline as below.
RewriteEngine on
RewriteBase /
#if asd or dfg or ghj
RewriteCond %{REQUEST_URI} ^/(asd|dfg|ghj) [NC]
RewriteRule . index.php?url=%{REQUEST_URI} [L]