PHP Routing - Disabling 404? - php

I'm creating a basic routing system, so my URL's would look like this:
www.domain.com/index.php/controller
Only problem is, when I go to that address, I get a 404. Is there any way to overwrite this with PHP (without using .htaccess)?

the index.php file would need to be your bootstrap file that would load the controller depending on what's being requested to it, but without using mod rewrite the url would be something like:
www.domain.com/index.php?url=controller

I've set up something like this before - In mine I had to have apache do redirects with mod_rewrite: mine sends www.domain.com/controller to www.domain.com/index.php?route=controller - should be similar to what you're after I think.
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\?*$ index.php?_route_=$1 [L,QSA]
Hope that helps.

Related

RewriteRule Not Working in .htaccess file

I'm trying to change the URL of my website to show only the ID but It seems to not work...
I don't know why.. other commands of RewriteRule work well..
Actually the .htaccess file looks like belove
RewriteEngine On
RewriteRule ^/?([0-9a-zA-Z-]+)/$ article.php?articleId=$1 [L]
I want that it works like this:
Old_URL(to modify):
mywebsite.it/article.php?articleId=15
I want something like this:mywebsite.it/article/15
But the URL remains the same actually: always display this: mywebsite.it/article.php?articleId=15
Thanks in advance to every help :)
An internal rewrite will never change the URL visible in the browser. You are probably looking for an external redirection. Or better the combination of both:
RewriteEngine On
# externally redirect old URL
RewriteCond %{QUERY_STRING} (?:^|&)articleId=(\d+)(?:&|$)
RewriteRule ^/?article/?$ /article/%1 [R=301,L]
# internally rewrite new URL
RewriteRule ^/?article/(\d+)/?$ /article.php?articleId=$1 [END]
Those rules are meant to be implemented on top level. Best in the actual http server's host configuration. If you do not have access to that then a distributed configuration file will work (".htaccess") when located in the host's DOCUMENT_ROOT, but support for that needs to be enabled.
It is a good idea to start out using a R=302 temporary redirection and only change that to a R=301 permanent redirection once you are happy with how things work. That prevents caching issues on the client side.

shorter url with php

I've seen it before and I have no clue how to do it or what to look for on google to find it.
Basically I'd like to know how to get these shortened urls to work, what i mean is for example
this: http://website.com/index.php?id=666
turned into this: http://website.com/666
Is there some way to do this through php or do people actually go around making maps for each id?
thanks
Hi there depends what server you are on but if you are using a server with apache that has mod rewrite you can use a htaccess rule.
So an example would be:
first create a .htaccess file then paste in the following content:
RewriteEngine on
RewriteRule ^([^/\.]+)?$ index.php?id=$1 [L]

.htaccess remove extension while preserving uri parameters

i was trying to rewrite my urls in a friendly way; i have written all the php code to handle everything.
The only thing that i am unable do is removing the .php extension while preserving the uri parameter.
So, essentially, i'm looking for some .htaccess rules to change from this:
www.example.com/biography.php/john-doe
to this:
www.example.com/biography/john-doe
(john-doe isn't a real file, it's only the GET parameter passed to biography.php)
Thanks everyone.
If the links on your site are already like this
www.example.com/biography/john-doe
and you want to process them on the server like this
www.example.com/biography.php/john-doe
add the following to the .htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
RewriteRule ^([-a-zA-Z]+)/([-a-zA-Z]+)$ $1.php/$2 [L]
Should do:
RewriteRule ([^\.]).php(.*) $1$2 [QSA,R=301]

How to understand PHP's URL parsing/routing?

I just inherited a website built in PHP. The main page of www.mysite.com has a href to www.mysite.com/index/35.html somewhere in the page. In the site's root directory and its children there is no document 35.html.
The number 35 is actually an id found in a DB which also holds the html contents of the page.
If I load URL: www.mysite.com/index.php?id=35 the same page loads.
How does PHP know how to automatically convert
/index/35.html
to
/index.php?id=35
EDIT
Based on the answers, I have found a .htaccess file containing rewrite instructions that would explain the functionality.
However, IIS doesn't seem to (or is not configured) know how to use this. (probably because this is an Apache feature?)
So this begs the following question: Is there a way to configure IIS to work with this?
it will be done usign URL Rewriting using .htaccess - should be in the webroot.
It may look something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
May have other bits, but what this basically tells apache is to send anything that DOES NOT physically exist to index.php
It doesn't. There is a mod_rewrite rule that rewrites from /index/foo to /index.php?id=foo, either in a .htaccess file somewhere or in the httpd configuration itself.
RewriteEngine On
RewriteRule ^index/([\d]+)\.html /index.php?id=$1 [NC,L]
This is off the top of my head. Any browsers trying to load an address starting with index/ has any number ending in .html will be internally redirected to index.php?id= whatever the number is.
Edit: Just saw that your working on IIS. This probably won't work for you. Sorry.
I think you will be using .htaccess to redirect all requests to index.php. From there You can pass the query string a routing class, which will parse the url and identify the unique ids.
In this case we can say like, your routing class will parse the request /index/35.html to indexController, indexAction, id=35. now you can pass this id to the model to get corresponding page contents
NB : Here I a am assuming you are using mvc pattern. Anyway it can be treated in your own way, with the concept remaining the same. Hope this make sence.

.htaccess 404 page not found

I'm writing my own url shortener. I'm done with everything such as creating short urls. But when I try to browse htt p://example.com/rtr93, I get a 404 error. But http://example.com/index.php/rtr93 works find and shows the relevant page (I'm not redirecting to a new url. I'm just getting the relevant record from database which has a column short_url).
I'm using PHP and syfmony 1.2 if that helps. I think I need to properly setup .htaccess file. But I don't know where to get started.
Something like this should work:
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ /index.php [L]
You may want to make the regex more specific if you're planning on hosting other things on the same domain.

Categories