HTACCESS not redirecting in server - php

This is my htaccess code:
RewriteEngine On
RewriteBase /visio
# Turn on the rewriting engine
# RewriteRule [region]?.html$ index.php/login/getProvinces [L]
# RewriteRule post_([0-9]+).html$ index.php?admin/index/url=$1
RewriteRule ^([^.]+)$ index.php/admin/index/$1
When iam try to load this url :
http://myserver.net/visio/test
I got the 404 not found error.In this url visio is the directory of my site in the server.When iam loading this url i want to go to the admin controller's index().
How can i do this?
If there is any mistake in my code?
This is mmy index() code:
function index(){
if(isset($_GET['url'])){
$newkey = $_GET['url'];
$data['result'] = $newkey;
$this->load->view('index',$data);
}else{
redirect('admin/index_login');
}
}
I want to get the url value in index() also.
But when iam changing the url like this:
http://myserver.net/visio/?test
Then it will goes to the index().So what is the problem in code?

^([^.]+)$ is a regular expression that matches "everything that is not any character". In other words, it will only match "nothing, once or more" (the dot symbol). If you meant to match actual dots, add a backslash in front of it.

Related

Codeigniter: The requested URL was not found in the server

I am not good in English so consider my grammatical mistake.
I am from Bangladesh so my client wants Bangla URL for SEO friendly. I tried to passing some particulars Unicode word in URL.
Normally everything is ok but when I pass this 'অর্থনীতি' types of word browser show object not found. I used url_encode/url_decode, permitted_uri_chars, and more other suggestion too but this browser message is same. can I pass this types of word in the URL?
I attached two picture. one is URL working fine and another is not working well.
**** Solution ****
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /example3/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /example3/index.php [L]
</IfModule>
first of all make sure you enable query string in config.php file
$config['enable_query_strings'] = TRUE;
Here you have to change your controller function.
public function news_details($news_id, $news_slug)
{
$data = array();
$data['news'] = $this->WelcomeModel->full_news($news_id);
$this->load->view('full-news', $data);
}
You just need to add this parameter $news_slug in your controller function
Edit 1 :
This is my function I tried :
function news_details($id, $slug){
print_r("News Id : ". $id);
print_r("<br>");
print_r("News Slug : ".$slug);
}
And this is what my output is :
Edit 2 :
Second screenshot as per your provided string

.htaccess rewrite to access php include in subfolder

I have this URL that give me two main parameters, folder and page, and som other tahat can variable to be present or not:
http://www.domain.com/adm/master.php?f=folder&p=page&otherparam1=somthing&otherparm2=somthing&otherparm3=somthing[...]
in my master.php-file I include a php-file based on these parameters:
$folder = $_GET['f'];
$page = $_GET['p'];
if(empty($page)) {
$page = 'dashboard';
}
$path = $folder.'/'.$page;
include_once 'pages/'.$path.'.php?otherparm1=somthing&otherparm2=somthing&otherparm3=somthing[...]';
The otherparam is to be multiple parameters given i the URL.
I want to rewrtite the URL to show somthing like this:
www.domain.com/adm/folder/page/somthing/somthing/somthing[...]
I have searched the web, but not been able to find a solution.
If you want to do it with simple .htacces then you can use following .htacces code:
RewriteEngine on
RewriteCond $1 !^(index\.php|css|favicon.ico|fonts|images|js|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
Basically what this code do is overwrite your request to your domain (ex: localhost/mysite), instead accessing folder or file it will access your index.php. Except for index.php, favoicon.ico, robots.txt files and css,fonts,image and js folder or what you define in second line on my code above.
Try this in your Root/.htaccess
RewriteEngine On
RewriteRule ^adm/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /adm/master.php?f=$1&p=$2&otherparam1=$3&otherparm2=$4&otherparm3=$5 [NC,L]
$1 ,$2,$3... contains whateve value was matched in regex capture groups "([^/]+)".
This will rewrite
example.com/adm/folder/page/foo/bar/foobar
to
example.com/adm/master.php?f=folder&p=page&otherparam1=foo&otherparm2=bar&otherparm3=foobar
without changing the url in browser address bar.

HTaccess 404 with mod_rewrite

I've got a small problem with my HTaccess urls. I wanted to make my URLs more SEO friendly, my current URL is:
url.com/blogitem/2
url.com -> Domain ; blog -> blogitem.php ; 2 -> ID for the blog item to get from the DB
Now I would like the URL to be like this:
url.com/blogitem/2/name-title-info
I've made the HTaccess part and the PHP part for it, but it will give me a 404 error page in return every time I visited the URL.
The HTaccess part:
# Blog SEO Urls.
RewriteRule ^blog/([0-9]*)/?$ blog.php?page=$1 [NC,L]
RewriteRule ^blogitem/([0-9]*)/?$/([a-z]*)/?$ blogitem.php?id=$1&name=$2 [NC,L]
The PHP part:
$trancTitle = trim(strtolower($blogTitle));
$underscoreTitle = preg_replace('/\s+/', '-', $trancTitle);
<a href="blogitem/<?php echo $blogId; ?>/<?php echo $underscoreTitle;?>">
Can someone explain why I'm getting an error page in return(404 to be exactly). I'm sorry for my bad English, it isn't my mother tongue. Thanks for helping in advance!
Your regex doesn't seem right, try these 2 rules:
RewriteEngine On
RewriteBase /
# Blog SEO Urls.
RewriteRule ^blog/([0-9]+)/?$ blog.php?page=$1 [NC,L,QSA]
RewriteRule ^blogitem/([0-9]+)/([\w-]+)/?$ blogitem.php?id=$1&name=$2 [QSA,NC,L]
The url that you are showing does not have "/" at the end: url.com/blogitem/2/name-title-info
Then you have mentioned blog.php in your .htaccess I think you are wanting to have this url too url.com/blog/2
So, you need to change a little bit you regular expression, try to putting this in your .htaccess:
# Blog SEO Urls.
RewriteRule ^blog/([0-9]+)$ blog.php?page=$1 [L]
RewriteRule ^blogitem/([0-9]+)/([a-z]+)$ /blogitem.php?id=$1&name=$2 [L]
Then in your php you will receive the variables via GET method
Try this to see that:
<?php
echo '<pre>';
print_r($_GET);
echo '</pre>';
?>

PHP Front Controller to get the Contents using HTACCESS file

I was trying to get make pretty URL. I have added a front end controller which gets the URL
and here is the Code for HTACCESS file and controller
RewriteEngine on
RewriteCond $1 !^(index\.php|search\.php|lib|css|ajax|includes|js|classes|parts|pages|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
Controller file
$URL = $_SERVER["REQUEST_URI"];
$all_parts = explode("/", $URL);
My URL is something like
www.example.com/pretty-ulr/10.html
I get the page id which is page.php?p_id=10 (This is a simple example. I am using more complex example. )
But I am not sure how to get the contents here or redirect t0 page.php?p_id=10 to get show this page contents. When I redirect using header(), It does not get there
May be something small in .htaccess file, but I am not sure. Any idea please how to get the page contents.
I recommend using something like this in .htaccess:
RedirectMatch ^/$ /index.php
Then in php do:
$url=parse_url($_SERVER['REQUEST_URI']);
$urls=explode('/',rawurldecode(trim($url["path"],'/')));
new Controller($urls);
The class Controller would parse the urls and get the right view.

URL Manipulation PHP

I have a url something like
http://something.com/abc/def/file.php/arguments
This simply executes final.php and /arguments is passed to $_SERVER['PATH_INFO'] variable.
I want to execute the same but without the '.php' i.e,
http://something.com/abc/def/file/arguments
I am guessing I need to add something to http.conf, or...?
.htaccess is your friend
Options +FollowSymLinks
RewriteEngine on
RewriteRule file/(.*) file.php?param=$1
I think the best way to do this is to adopt the MVC style url manipulation with the URI and not the params.
In your htaccess use like:
<IfModule mod_rewrite.c>
RewriteEngine On
#Rewrite the URI if there is no file or folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Then in your PHP Script you want to develop a small class to read the URI and split it into segments such as
class URI
{
var $uri;
var $segments = array();
function __construct()
{
$this->uri = $_SERVER['REQUEST_URI'];
$this->segments = explode('/',$this->uri);
}
function getSegment($id,$default = false)
{
$id = (int)($id - 1); //if you type 1 then it needs to be 0 as arrays are zerobased
return isset($this->segments[$id]) ? $this->segments[$id] : $default;
}
}
Use like
http://mysite.com/posts/22/robert-pitt-shows-mvc-style-uri-access
$Uri = new URI();
echo $Uri->getSegment(1); //Would return 'posts'
echo $Uri->getSegment(2); //Would return '22';
echo $Uri->getSegment(3); //Would return 'robert-pitt-shows-mvc-style-uri-access'
echo $Uri->getSegment(4); //Would return a boolean of false
echo $Uri->getSegment(5,'fallback if not set'); //Would return 'fallback if not set'
Now in MVC There usually like http://site.com/controller/method/param but in a non MVC Style application you can do http://site.com/action/sub-action/param
Hope this helps you move forward with your application.
So you want to rewrite your URL's. Have a look here: http://corz.org/serv/tricks/htaccess2.php
This URL style can be managed by the url_rewrite (called url rewriting) and it can be done with .htaccess file of your Apache server.
to do it you'll need to write this in you .htaccess file:
RewriteEngine On
RewriteRule ^http://something.com/every/name/you/like/(arguments)/?$ server_folder/page.php?argument_var=$1
The first block of code, rapresents the user called page:
^http://something.com/every/name/you/like/(arguments)/?$
The second block is the real page you want to call, where $1 is the var value inside the ()
server_folder/page.php?argument_var=$1
If the user must go to an URL where the arguments are numbers only you should insert:
^http://something.com/every/name/you/like/([0-9])/?$
If the user must go to an URL where the arguments are letters only you should insert:
^http://something.com/every/name/you/like/([a-zA-Z])/?$
To work correctly with this URL style you'll need to understand a little bit of regular expresions like in this link.
You could find useful this table to help you understand something more.
Note you can write different URL instead of the real page name like:
^http://something.com/love/([a-zA-Z0-9])/?$ section/love/search.php?$1
This should be useful to hide server pages.

Categories