.htaccess PHP Controller Internal Server Error - php

I'm creating a PHP controller for testing purposes on my domain. The domain, by default, is entirely on Wordpress and I'm having issues setting up just the folder testing1 to be controlled by my PHP controller. Here's the code in my .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
RewriteRule ^testing1/(.*)$ ./testing1/controller.php
</IfModule>
# END WordPress
When I go to mydomain.com/testing1/ I get an internal server error. Any help is appreciated. Thanks!

Get rid of ./, which is used to point to current directory.
RewriteRule ^testing1/(.*)$ testing1/controller.php
I have it working.

The RewriteRules are executed in order. If one does match, the rest is ignored.
So the result might look like this (just the RewriteRules):
RewriteRule ^testing1/(.*)$ /testing1/controller.php
RewriteRule . /index.php
The second rule does mean that when you type exactly one character (whatever it is), you will get directed to index.php. I don't know though if that is your intended behaviour.

Related

Treat .php File as Root Folder

So right now I have a index.php file that acts as an API in the root of a subdomain.
So if you go to http://some.domainname.com/index.php/items/ for example you get a response.
However if you go to http://some.domainname.com/items/ you get a 404 error.
I'm assuming there is some .htaccess rewrite command I can add to make this work but I haven't had any luck finding it.
Here is an example based on how Magento does it:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
It will effectively remove the index.php from your urls, but it won't stop anyone from accessing a file that actually exists, like www.example.com/i-exist.php
Note:
This is specifically geared toward a site whose urls alway end with a trailing slash (/), like www.example.com/index.php/somepage/. This is set with the RewriteBase / line.

htaccess and pretty links

I'm trying to write a simple blog which is working fine untill i get to the pretty links thing.
what i'm looking to do is use URLs like mysite.com/blog/this_post and pass that into the index.html file as a url parameter so index.html?blog=this_post
Been searching everywhere and found a bunch of htaccess code but most didn't work. I did find one solution that tries to work but for some reason isn't doing it correctly. (with and without the Options +FollowSymLinks part)
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^blog/(.*) /blog/index.html?blog=$1
when I use this i get /blog/index.html?blog=index.html in the url instead of keeping the /blog/this_post and porting that into the index.html as expected. Could something on the server be set incorrectly? everything else with the blog is working like a charm but so nothing wrong with the script its just getting the pretty links to work.
Looked at an older site that has wordpress and coppied over that htaccess code but it does that same thing. All the other searches on here and other sites point to the same solution above or the wordpress but for some reason it's passing index.html to the script and not the last url segment "this_post" as expected.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.html [L]
</IfModule>
# END WordPress
what should happen is the user types in
mysite.com/blog/this_post
which remains in the address bar correct? but the htaccess file pulls this_post and passes it as an argument to the index.html script. which is what the examples above should do. why it's changing the address bar to
mysite.com/blog/index.html?blog=index.html
i'm not sure why
side note, since i got this before...yes i'm aware of using the .php extention but .html is a client ask that they're not budging on.
edit-the link for possible duplicate was one I already found and tried to get the current RewriteRule but it's still not working.
The best way is to made something simple like this:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([^/]*)\.html$ index.php?blog=$1 [L]
Options -MultiViews
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
then you can do URL like:
mysite.com/this_post
what is equal to:
mysite.com/index.php?blog=this_post
Like that you can do both calls and will work fine.
Generaly you need first decide how you want your SEO URL to look like and then you need to setup .htaccess regex and rules.
NOTE: you can't use .html for GET calls. You can use .php files for your works and use $_GET['blog'] to pickup your data from URL.
Seem to have found the combo that works, the [NC, L] flag combo keeps giving me a crash error so i left it just [NC]. Having it in the root account directory seems to not work also so i moved into the web directory (public_html) and that works for now.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/(.*) /blog/index.html?blog=$1 [NC]
Still not sure why the L flag gives me a server crash on the pages or why having it in the root doesn't work but moving it up into the public_html folder does, is it possibly a server configuration problem that i'm overlooking?

Getting Laravel htaccess to listen to my rewrites

I'm trying to rewrite some old site pages to routes on my shiny new Laravel site. I understand in principle how .htaccess works, but there's a line in the default Laravel .htaccess which (I think) is throwing my own rewrites. So, this is something like what I am trying to do:
RewriteRule ^user/(\d+)*$ ./users
However, Laravel already has this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Which effectively says "If the user request is for any file or folder that does not exist, pipe their request through to index.php" - the [L] meaning that this is the last rule for these conditions.
My problem with this is, where do I put my rewrite instructions? If I put it after that rule then surely my rewrite will be ignored as everything is already being sent to index.php (if htaccess rules are sequential)
If I put my rule before Laravel's default rules, does that mean that my rule will mess up what Laravel is trying to do? At the moment, wherever I put my rule, I just get various result ranging from a 500 error to a blank page with no content.
How can I integrate my own rules into Laravel's .htaccess file without stepping on Laravel?
To make sure that Laravel's .htaccess file rules do not conflict with yours, you need to ensure that your rules come before theirs. I am supplying a sample of my .htaccess files with path names changed, each line annotated so that it makes sense to those who are facing the sam problem:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
# Make sure Rewrite engine is running
RewriteEngine On
# Make sure base of site is set to root (othwerwise you can get results like /var/www/public_html/ before the results pages)
RewriteBase /
# For all directory requests in the url (d) e.g. blah.com/directory/
RewriteCond %{REQUEST_FILENAME} !-d
# And for all file requests in the url (f) e.g. blah.com/directory/page.php
RewriteCond %{REQUEST_FILENAME} !-f
# Check for the CONDition that the page has 'product_id' in the url e.g. http:/www.blah.com/show_product.php?product_id=273
RewriteCond %{QUERY_STRING} ^product_id=(.*)$
# If the condition above was met, then check if the page is called list_products.php, if it is, redirect the browser to /product/ with the product id (273) as %1
RewriteRule ^list_products.php$ product\/%1? [R=301,L]
# Simpler redirect for pages without a url variable
Redirect /car-hire.php https://www.blah.com/car-hire
# Then comes Laravel’s .htaccess stuff
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
It seems (as CBroe kindly answered in his comment) that keeping your rules ‘on top’ of Laravel’s as above is the answer

How to redirect static to dynamic URL's using .htaccess (on Wordpress site)

I've taken over a former site/domain, and set up a new site using Wordpress. The WP installation rewrites URL's to static ones, as you'd expect it to.
At the same time I want to preserve the former pages, as they have incoming links. I'm not interested in 301'ing them to "new" pages.
The old URL structure is /index.php?id=123, which I suspect is causing the problem with the WP .htaccess file. For reference, this is what it looks like:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I've tried adding the following:
RewriteRule ^([0-9]+).html index.php?id=$1 [R,L]
Doesn't work. Just redirects to site.com/?id=123 and shows the front page.
I should add that I plan on just adding these new pages as regular static HTML files in the format of 123.html, 321.html etc.
How do I use .htaccess to make this work together with the WP installation and what WP puts into the .htaccess file?
To clarify:
I want to have my 123.html static HTML page be index.php?id=123. When you access index.php?id=123 it should bring up 123.html, but show index.php?id=123 in the address bar. If you access 123.html it should 301 to index.php?id=123.
To map an URL with a querystring up to an actual file you'll need to use a RewriteCond to match the querystring itself (as RewriteRule doesn't):
Something along these lines ought to do it:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# retrieve X.html when index.php?id=X is requested
RewriteCond %{REQUEST_FILENAME} index\.php
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteCond %1.html -F
RewriteRule .* %1.html? [L]
# standard WordPress routing
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This will first check to see if you've got a request for index.php with a querystring like id=X.
Then it'll check to see if a file called X.html actually exists; I'm not 100% happy about having to use the more system hungry subrequest file check -F rather than the standard -f but I can't see a way around it in .htaccess in this case.
If X.html actually exists, it'll fetch that file whilst leaving the URL as index.php?id=X.
However if that file doesn't exist it'll fall back to standard WordPress no file, no directory routing to index.php
I'm not a WordPress expert but that should work; I guess the main WordPress controller uses $_SERVER['REQUEST_URI'] to determine the action.
Note: This won't, however, prevent people from accessing 123.html directly by going to the URL www.site.com/123.html - I kept falling into infinite loops and Apache 500 errors trying to prevent that :|

How can I display the resulting path when using mod_rewrite?

I am having a heck of a time trying to get an apache mod_rewrite rule to work. The thing that is making the process the most difficult is that I have no way of knowing what the final output string is. I continue to get 404 pages, but that doesn't tell me much except that I did something wrong. Is there some method of echoing out the final rewrite rather than redirecting me to a 404?
. . . And just in case someone feels like helping me out with my original problem, I'm trying to make it so that anyone who goes to /server/root/to/folder/public_html/ gets redirected to /server/root/to/folder/public_html/destinationFolder/. Below is one of the hundreds of variations that I've tried:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !^(favicon\.ico|assets)
RewriteRule ^public_html/(.*) destinationFolder/$1 [L]
</IfModule>
You are using system paths. Apache mod_rewrite only works with URLs (PT flag is implied in .htaccess files), that's your first error.
public_html is normally the root directory. For example: http://mydomain.com
Then, a complete rule set could be something like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(favicon\.ico|assets) [NC]
RewriteRule ^(.*)/?$ DestinationFolder/$1 [L]
Maps silently
http://mydomain.com/Anything
To:
http://mydomain.com/DestinationFolder/Anything
Except when anything is file favicon.ico or folder /assets
For permanent redirection, replace [L] with [R=301,L]
This is just an example. The rules and parameters have to be modified to meet the real requirements.

Categories