I have url given below
http://localhost/main20/pages/view.php?id=hello-world
and want it to be shown as
http://localhost/main20/hello-world
I know its done using .htaccess and have found something here URL rewriting with PHP but its not fulfilling my requirement as my text "hello-world" will be changing on every request. Its something similar that wordpress does.
i found a solution my self here is the code and this http://roshanbh.com.np/2008/03/url-rewriting-examples-htaccess.html link solved my issue.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ pages/view.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ pages/view.php?id=$1
NOTE: I am assuming that in http://localhost/main20/ will have index.php if not then create one. It will look a like something http://localhost/main20/index.php.
Now your .htaccess file would be something.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [NC,L]
Options All -Indexes
In your index.php
<?php
define('WEBROOT','http://localhost/main20/');
//assuming your url is "http://localhost/main20/hello-wolrd"
$link=$_SERVER['REQUEST_URI'];
$linkArr=explode("/",str_replace(WEBROOT,"","http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']));
/*
print_r($linkArr);
will output
Array ( [0] => hello-wolrd )
*/
//now chech if $linkArr[0] is empty or not
if(!empty($linkArr[0]))
{
include("pages/view.php?id".$linkArr[0]);
}
?>
Hope this help you.
This is a good link for more information.
I think this .htaccess should work for you. Put it in the same folder where index.php is
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ pages/view.php?id=$1 [L,QSA,NC]
Related
I'm having trouble rewriting a URL on my website to make it more presentable and easier to find on search engines.
I want to turn this:
http://www.gamingpopulace.com/threads/index?threadName=Glitches
Into this:
http://www.gamingpopulace.com/threads/Glitches
I'm currently using a .htaccess file with this in it:
RewriteEngine On
RewriteRule ^threads/([A-Za-z0-9-]+)/?$ /threads/threadName=$1 [NC,L]
From what I've seen seen in other questions, this should work.
What goes wrong:
When I type http://www.gamingpopulace.com/threads/Glitches into the URL, it just gives me a 404 error saying that the page is missing. From my understanding it should load http://www.gamingpopulace.com/threads/index?threadName=Glitches, but with the changed URL. Though I might be misunderstanding that.
Any help is appreaciated.
Thanks
Hello and welcome to SO
(I cheated and used Google but amended the info according to your question.)
Source : How To Set Up Mod_Rewrite
RewriteEngine on
RewriteRule ^thread/([A-Za-z0-9-]+)/?$ threads/index?threadName=$1 [NC]
Remove / from this ([A-Za-z0-9-]+)/?$
Replace
RewriteEngine On
RewriteRule ^threads/([A-Za-z0-9-]+)/?$ /threads/threadName=$1 [NC,L]
with
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^threads/([^/]*)?$ /threads/threadName=$1 [L]
I am assuming your original url is working, try this in root dir,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^threads/([\w-]+)?$ threads/index?threadName=$1 [QSA,NC,L]
I'd like to make it so when someone does example.com/page.php/query it works as a $_GET (example.com/page.php?q=query) for the query
How would I do this?
Would this be done with the page.php's code or would I do it in .htaccess?
In order to route a request like /test/something to internally rewrite so that the content at /test.php?whatever=something gets served, you would use these rules in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?test/(.*?)/?$ /test.php?whatever=$1 [L]
I just started .htaccess so I do not know too much about it, I try to google it but failed so I post my question here, I am sorry if the question is silly for you. My question is.
I use this code in .htaccess page:
RewriteEngine On
RewriteBase /learn/php/htaccess/
RewriteRule ^(.*)/(.*)$ index.php?n=$1&p=$2
when I enter this url:
http://localhost/learn/php/htaccess/page/number
it works fine. but I also want to add index.php (page name) in the url. I tried this:
http://localhost/learn/php/htaccess/index.php/page/number
AND
http://localhost/learn/php/htaccess/index.php?page/number
but failed. How can I do this?
I simply want to do that if I add another page then I am not sure but I have to use this code:
RewriteEngine On
RewriteBase /learn/php/htaccess/
RewriteRule ^(.*)/(.*)$ index.php?n=$1&p=$2
RewriteRule ^(.*)/(.*)$ another.php?x=$1&y=$2
but how can I open the another.php page with this format?
thanks
Try adding a few conditions to your rules:
RewriteEngine On
RewriteBase /learn/php/htaccess/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ index.php?n=$1&p=$2
As for routing to another page, you'll need to distinguish the difference between the two routes. For example, given a url:
http://localhost/learn/php/htaccess/foo/bar
where should it get routed to?
index.php?n=foo&p=bar
or
another.php?x=foo&y=bar
?
htaccess knows nothing about the content, only the URL and a regex pattern to match it, so unless you differentiate between the two, you can't route the same thing to two different scripts.
You can just make it easy like this
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?data=$1 [QSA]
in your index.php you put this code
$data = explode("/",$_GET['data']);
$page = $data[0];
$number = $data[1];
I have this .htaccess that I've been using to rewrite URLs like these:
www.example.com/index.php?page=brand www.example.com/brand
www.example.com/index.php?page=contact www.example.com/contact
www.example.com/index.php?page=giveaways www.example.com/giveaways
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
I used a file called index.php to handle the redirects. Code used below:
$page = trim($_GET['page']);
if($page == "giveaways")
require('pages/giveaways.php');
Now, I would like to add another URL type like these:
www.example.com/index.php?page=products&p=ford-mustang
TO
www.example.com/products/ford-mustang
How will I accomplish this? Any suggestion would be greatly appreciated.
You need to add a second RewriteRule above your current one.
Here's an example:
RewriteRule ^/(.*)/(.*)$ index.php?page=$1&p=$2 [L,QSA]
This will rewrite /product/ford-mustang to index.php?page=product&p=ford-mustang
Remember to add it above your current RewriteRule, because it first tries to match the first RewriteRule, when there's no match it will go on with the second RewriteRule and so further.
A URL rewrite for /products/ford-mustang would be:
RewriteRule ^(.*)/(.*)$ index.php?page=$1&p=$2 [L,QSA]
So i have a mvc system setup but it does not generate search engine friendly urls.
A typical url is in the format:
http://sitedomain.com/class/classMethod?parameter=valueA?parameter2=valueB
This is what i need to have:
http://sitedomain.com/class/valueA/valueB/
My .htaccess actually modified a part of the url already but i dont know how to do the second part
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?controller=$1 [L,QSA]
(originally looked like http://site.com/index.php?controller=class, but after the htaccess below is ran, it looks like http://site.com/class)
If anyone could help me with this, that would be great, thank you.
RewriteRule ^/class/(.*)/(.*)/$ index.php?controller=class¶meter=$1¶meter2=$2 [L,QSA]
I use the following .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ rewrite.php?%{QUERY_STRING}
And extracting parts from the url is all done in PHP.
Parsing the $_SERVER['SCRIPT_NAME'] variable.
(I find php code much easer to debug than complex apache rewrite rules.)