.htaccess 404 page not found - php

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.

Related

Rewrite URL with parameters passed through PHP

So I have my PHP page page.php with the parameter id for example.
Everything works as expected when I visit
page.php?id=1
What I try to do now basically is, reach the site when the user visits
page/1
I've looked at countless questions and websites about htaccess rewrites and so on, but I'm not even entirely sure if I need that. I did the exact same thing in an old project of mine where I can't find any htaccess file, but I also have no idea what I really did.
And in case I do need the htaccess file. How do I ensure that it works properly?
Alright, gotta put this in the htaccess
RewriteEngine On
RewriteRule ^page/([^/]*)$ /page.php?id=$1 [QSA,L]

Get page id value and display in URL via .htaccess

I'm trying to display SEO friendly URLs by using a rewrite in our .htaccess file, but I can't get it to work (I've researched many of the related topics on StackExhange and elsewhere, but to no avail). I'd like to get the value of the id on this page...
http://199.119.123.135/info/tool_surety_company.php?id=1
...and display the id value in the URL instead of the ugly "tool_surety_company.php?id=1".
I'm going for a result like this: http://199.119.123.135/info/travelers-group
I'm using the following code in my .htaccess file:
RewriteCond %{THE_REQUEST} \ /+info/tool_surety_company\.php\?id=([^&]+)
RewriteRule ^ /info/%1/? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^^info/([^/]+)/?$ /info/tool_surety_company.php?id=$1 [QSA]
But I'm receiving a 404 error.
Any ideas? Thanks in advance!
There might be something I'm misunderstanding here but I believe there would need to be a mechanism on the server side code to load the correct content for the new "seo-friendly url". In other words, sure, you can redirect the user to show a different url but how is the server going to know what content to load for that new url?
Here's a good resource for putting together a simple example.
https://moz.com/ugc/using-mod-rewrite-to-convert-dynamic-urls-to-seo-friendly-urls
Update:
From here - https://mediatemple.net/community/products/dv/204643270/using-htaccess-rewrite-rules
TROUBLESHOOTING
404 Not Found
Examine the new URL in your browser closely. Does it match a file that
exists on the server in the new location specified by the rewrite
rule? You may have to make your rewrite rule more broad (you may be
able to remove the $1 from the second string). This will direct
rewrites to the main index page given in the second string. Or, you
may need to copy files from your old location to the new location.
In other words, the only reason you would be getting a 404 is because the server does not find the file that is requested as defined in the URL visible in your browser address bar.
Htaccess Rewrites are enabled by using the Apache module mod_rewrite,
which is one of the most powerful Apache modules and features
availale. Htaccess Rewrites through mod_rewrite provide the special
ability to Rewrite requests internally as well as Redirect request
externally.
When the url in your browser's location bar stays the same for a
request it is an internal rewrite, when the url changes an external
redirection is taking place. This is one of the first, and one of the
biggest mental-blocks people have when learning about mod_rewrite.
More info from here:
http://www.askapache.com/htaccess/modrewrite-tips-tricks.html

PHP Routing - Disabling 404?

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.

Codeigniter is not showing 404 error page

I'm developing a new website using codeigniter, this is my first codeigniter experience. I suspect that there is something not working with the 404 error, when I try to load a controller that does not exist, or a wrong function of an existing controller, I get back the standard webserver 404 error, and not the codeigniter one.
I did not touch the "404_override" option in the route config file.
Is it normal? I expect (but maybe I'm wrong) that in these cases the show_404() function is called.
Is your .htaccess file correctly set up?
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
You have to setup an .htaccess file in order to have custom error pages working. Checkout the official documentation.
Codeigniter should be showing it's 404 page, unless you did not remove the index.php from the URI. In that case you can try to open another file without touching CodeIgniter. Take a look here to see how to remove index.php using .htaccess.
Update:
There seems to be a solution for ISS: ISAPI_Rewrite Lite (free, lite version of a commercial product). I found it here

My PHP file is not parsing correctly

I am having a strange issue that I have never encountered in the past 10 years, please help.
I am working on a very simple site for a client, they have a shared host LAMP stack, I am using an .htaccess file like this...
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?p=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?p=$1
So I can access pages like domain.com/contact and it will go to domain.com/?p=contact pretty straight forward, however on the index page, domain.com it will not parse the PHP correctly, if I view source I can see my actually PHP code un-parsed on this page, all the other pages work except for my index page, any ideas why this would happen?
It worked fine on my localhost
Update
I can access domain.com/index and domain.com/index.php and they work fine, it is just when I access domain.com that it does not parse correctly
It looks like the server's setup is messing with DirectoryIndex option - it tries to find the "closest" file, but does not run php engine for it. Yes, it sounds "weird", but try to add this string to .htaccess:
DirectoryIndex index.php
ps: and another thing I wrote in comments - one RewriteRule is enough: RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?p=$1

Categories