How can I change page url query and get first two letters . in .htaccess or php
http://127.0.0.1/site/flowers/index.php?query=Bird
http://127.0.0.1/site/flowers/index.php?query=eagle
Like this :
http://127.0.0.1/site/flowers/search/bi/bird.html
http://127.0.0.1/site/flowers/search/ea/eagle.html
Htaccess code : 127.0.0.1/site/.htaccess
RewriteEngine on
RewriteRule ([^/\.]+)/?.html$ index.php?query=$1 [L]
Here is a Javascript code that you can use for this redirection:
<html>
<head>
<title>Form Post Example</title>
<script>
function prettySubmit(form, evt) {
evt.preventDefault();
var val = form.query.value.toLowerCase();
window.location =
form.action + '/' + val.substr(0, 2) + '/' + val.replace(/ /g, '+') + '.html';
return false;
}
</script>
</head>
<body>
<form method="get" action="flowers/search" onsubmit='return prettySubmit(this, event);'>
<input type="text" name="query">
<input type="submit" value="Search">
</form>
</body>
</html>
Then you can have this rule in site/.htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([^/.]+)\.html$ index.php?query=$1 [L,QSA,NC]
This will convert your URLs to:
http://127.0.0.1/site/flowers/search/bi/bird.html
When you perform search using word bird in search field.
Add the following to your .htaccess
RewriteRule ([^/\.]+)/?.html$ index.php?query=$1 [R=301]
RewriteCond %{QUERY_STRING} query=([^/\.]{2})([^/\.]+)
RewriteRule index.php search/%1/%1%2.html [R=301]
Then http://127.0.0.1/site/flowers/Bird.html redirects to http://127.0.0.1/site/flowers/index.php?query=Bird which in turn redirects to http://127.0.0.1/site/flowers/search/Bi/Bird.html
I'm assuming this is what you want?
Related
I have a HTML form containing a textbox and a submit button. When I submit the form, I want textbox value appending to the URL.
For example, if my domain is http://www.example.com/ and form lies on index.php. Suppose textbox value is "test" then when the form is submitted the URL should change to http://www.example.com/test. How can we achieve it?
HTML Code:
<form method="post" action="">
<input type="text" name="txtname" value="<?php echo $name; ?>" />
<input type="submit" name="btnsubmit" value="Submit" />
</form>
PHP Code:
<?php
$name = NULL;
if(isset($_POST['btnsubmit']))
{
$name = $_POST["txtname"];
}
?>
.htaccess Code:
# .htaccess mod_rewrite
# example.com
#Enable mod rewrite
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
How about a jQuery solution? Something like...
$(document).ready(function(){
var txtname = $('#txtname').val();
var url = window.location.href;
var new_url = url + "/" + txtname;
$('#formid').on('submit', function(e){
e.preventDefault();
// Do what you need to do here.
console.log(new_url);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="formid" method="post" action="">
<input id="txtname" type="text" name="txtname" value="testname" />
<input type="submit" name="btnsubmit" value="Submit" />
</form>
You could use header:
$name = $_POST['txtname'];
header('Location: http://www.example.com/$name');
If you want to get the value without the error you can set the variable to empty like this:
$name = isset($_POST['txtname']) ? $_POST['txtname'] : '';
Place that after your opening php tag.
I'm new to CodeIgniter. I have a homepage with form controller form. I want when click on submit in homepage redirect to signup function in controller form
But it redirect to http//localhost//xampp
This is Home_view view
<?php $id = 1; ?>
<form action="<?php echo base_url('login_c/signup/' . $id); ?>" method="post" >
<input type="submit" value="Sign up" />
</form>
This is form controller
public function index()
{
$this->load->helper('url');
$this->load->view('Home_view');
}
public function signup($id)
{
echo$id;
$this->load->view('myproject/Signup_v');
}
Yes problem may be with your .htaccess. Try with this once,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
I'm trying to validate a form that's being submitted through a PHP router.
All my traffic is sent to index.php via htaccess. to index.php:
RewriteEngine On
RewriteBase /router
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /divbase/index.php?/$1 [L]
index.php then provides the appropriate content:
<?php
Router::get('/form', function() {
include 'content/form.php';
});
?>
so when you go to example.com/form, the form page is loaded:
<?php
$errorMessage = false;
if (isset($_POST['submit'])) {
if (!isset($_POST['name']) || $_POST['name']=='') {
$errorMessage = 'Please enter your name';
}
else {
// do something with the data
echo "Success!!";
}
}
?>
<form method="post">
<input type="name" value="" name="name" placeholder="Your Name" />
<input type="submit" name="submit" />
</form>
<p><?php if ($errorMessage) echo $errorMessage; ?></p>
However, when I try to submit the form on that page it 404s. After doing some troubleshooting, it looks like the PHP thinks that the form is being submitted to index.php rather than form.php.
I've tried to set the form action to:
<?php "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>
But it still 404s. Does anyone have any suggestions for this?
You need to add another route for POST, for example...
Router::get('/form', function() {
include 'content/form.php';
});
Router::post('/form', function() {
echo 'Hello world';
});
Your form is using method="post" so you need to add a POST route
I was searching now quite a bit on the internet but can not find any solution.
I have an .htaccess file, which rewrites my query string into the GET variable.
This works absolutely fine.
when it comes to the POST variable, it is the first time I get into this trouble.
I have used this .htaccess file already a lot of times on different servers, but now one of my client's server doesn't want to work.
When ever I post a form the whole POST variable is empty... I will post my .htaccess file, any help is appreciated.
Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteBase /
RewriteRule ^pages.*$ /error/404 [L]
RewriteRule ^classes.*$ /error/404 [L]
RewriteRule ^templates.*$ /error/404 [L]
RewriteRule ^common.*$ /error/404 [L]
RewriteRule ^([A-Za-z0-9-_]+)/?$ /?p=$1&s=index&a=index&n=index&%{QUERY_STRING} [L]
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ /?p=$1&s=$2&a=index&n=index&%{QUERY_STRING} [L]
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ /?p=$1&s=$2&a=$3&n=index&%{QUERY_STRING} [L]
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ /?p=$1&s=$2&a=$3&n=$4&%{QUERY_STRING} [L]
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ /?p=$1&s=$2&a=$3&n=$4&o=$5&%{QUERY_STRING} [L]
ErrorDocument 404 /error/404.php
the form looks like this
<div class="login">
<form action="/administrator" method="post">
<table cellspacing="0" cellpadding="0" width="100%">
<tr>
<td>Username</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="passwort" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Anmelden" /></td>
</tr>
</table>
</form>
</div>
and the login php file like this
<?php
session_start();
if( isset($_SESSION['login']) && $_SESSION['login'] === TRUE ) {
header('Location: /administrator');
exit;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = $_POST['username'];
$passwort = $_POST['passwort'];
$hostname = $_SERVER['HTTP_HOST'];
$path = dirname($_SERVER['PHP_SELF']);
if ($username == 'xxx' && $passwort == 'xxx') {
$_SESSION['login'] = true;
header('Location: /administrator');
exit;
}
}
?>
Your RewriteRule are active for GET, not for POST.
check if your /administrator script (why haven't you called it directly as administrator.php?) is called or create a rule for its access. Check in the access log to be sure you're calling the script when you post to it.
Add something like :
RewriteRule ^administrator administrator.php [L]
or RewriteRule ^administrator pathtoyourscript/administrator.php [L]
check without session to be sure it's not interacting badly with it.
You can use var_dump($_POST) to display all data. If there is no display, you've not called the right script or your path is wrong.
I have PHP code for shortening a URL. I want to integrate it with CodeIgniter. How can I do this?
I have 3 pages: index.php, page.php and a .htaccess file and one database backup file.
index.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("test");
$url=$_GET['url'];
$url_details=mysql_fetch_array(mysql_query("select * from tinyurls where shorturl like ('".$url."')"));
if(!empty($url_details)){
header("location: ".$url_details['actualurl']);
}else{
echo '<h2 style="color:red">Error 404: Page not found</h2>';
}
?>
page.php
<?php
if(isset($_POST['url'])){
mysql_connect("localhost","root","");
mysql_select_db("test");
$exist=mysql_fetch_array(mysql_query("select * from tinyurls where actualurl='".$_POST['url']."'"));
if(empty($exist))
{
function get_random_word($length=6){
$chars='abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$word="";
for($i=0;$i<$length;$i++){
$word.=substr($chars,rand(1,strlen($chars)),1);
}
$isexist=mysql_fetch_array(mysql_query("select id from tinyurls where shorturl like ('".$word."')"));
if(empty($isexist)){
return $word;
}else{
return "";
}
}
$tiny_word="";
while($tiny_word==""){
$tiny_word=get_random_word();
}
mysql_query("insert into tinyurls set shorturl='".$tiny_word."', actualurl='".$_POST['url']."'");
}else{
$tiny_word=$exist['shorturl'];
}
echo "TinyURL: http://".$_SERVER['SERVER_NAME']."/tinyurl/".$tiny_word."<br/><br/>";
}
?>
<html>
<head><title>Shorten URL</title></head>
<body>
<form method="post" action="">
Enter the URL:<br/>
<input type="text" name="url" style="padding:5px;width:500px"/><br/><br/>
<input type="submit" style="padding:5px" value="Shorten URL">
</form>
</body>
</html>
.htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^p\/(.*)$ page.php?q=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
How can I integrate it with CodeIgniter?
when iam integrating this with codeigniter the controller goes to the home page of my site not to the original url
In your code you're just echoing the URL instead of redirecting it:
header('Location: http://' . $_SERVER['SERVER_NAME'] . '/tinyurl/' . $tiny_word);