Rewrite Mode and PHP using .htaccess not working (Error 404) - php

Here is my .htaccess:
RewriteEngine on
RewriteRule !\.(js|ico|gif|ini|jpg|png|css|html|swf|flv|xml)$ index.php
I've been using this file for about a year now and frankly, if you ask me I'd say it means: If you're trying to access something NOT ended with js, ico, gif... flv, xml THEN redirect to index.php
Now I'm trying to deploy an application at a new server (centOS WITH mod_rewrite enabled) and I get the Error 404 when trying to access stuffs that usually works (at a different web server, for instance).
Since I know too little about it, I'd like some input on what should I be looking for to find the problem. Thanks in advance.

You should perform that kind of match in RewriteCond
RewriteEngine on
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !\.(js|ico|gif|ini|jpg|png|css|html|swf|flv|xml)$
RewriteRule .* index.php
Note: the first rule is needed so that the rules won't loop (exclude index.php itself from rewriting).

Related

htaccess - Access root subfolder

I believe this question is purely PHP related, a language in which my knowledge is limited and I'm still learning things, keep that in mind.
I installed Ionize CMS on a client's server and everything is going smooth. But there is one little thing I wish I could do and it reaches the extent of my PHP knowledge.
I want to know if it is possible to access the content of a folder located at the root of my site from my web browser, which is not ionize related. I need to access the old backup of a website. In other words, I want to be able to write in the url the path and see the content in the web browser. Ex:
http://mysite/backup/index.php
For the moment, if I try that, the page displays the ionize 404 not found message.
I guess it has something to do with the htaccess file, but I'm really not sure how to modify this file to do what I want, if possible.
I did a bit of research and found this code, but if someone could explain it to me that would be great, as my php knowledge is not that great like mentioned earlier. I don't want to test it right now without knowing what it does, I don't want to permanently damage my website rewrite function..
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/subdirectoryname1/(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^/subdirectoryname2/(.*)$ [OR]
RewriteRule ^.*$ - [L]
</IfModule>
Anyone could spare some knowledge on the feasibility of my question?
Much appreciated
This rule basically says if the requested URI (folder) is subdirectoryname1 or subdirectoryname2don't do anything and stop processing the rules. So a rule like this should work for you. The rules have to come before your ionize rules.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/subdirectoryname1/(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^/subdirectoryname2/(.*)$
RewriteRule ^.*$ - [L]
</IfModule>
You can also just do this for your situation which does the same thing.
RewriteEngine On
RewriteBase /
RewriteRule ^backup/?(.*?)/?$ - [L]

Redirecting all requests to index.php with exceptions

I've searched everywhere trying to find a solution to something I thought was a particularly common problem, but I can't seem to find anything that works.
I'm using mod_rewrite to redirect all requests via my own CMS (index.php). Here's the htaccess code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
(The above mod_rewrite is exactly the same as the code found in a standard Wordpress installation which is why I'm perplexed as to how I cannot find a solution for the following problem)
I want to add some image processing to my video thumbnails. I have written a php script (video-image.php) that works great but I cannot seem to find a means to redirect the images urls whilst keeping the index.php redirect intact. The code I'm looking for does something like this:
RewriteRule ^images/video/(.*)/(.*).jpg /images/video/video-image.php?video_image_id=$2&video_image_width=$1 [NC]
But the above code seems to clash with the mod_rewrite that sends my requests to index.php. It seems I can either redirect the images to video-image.php or redirect my pages to index.php but there must be a way to do both?
I've always found mod_rewrite confusing so apologies if I'm not explaining myself clearly enough. All php scripts function perfectly well without the mod_rewrite so I'm certain it's not a php issue.
If anyone can shed any light on this problem or point me to an answer I'd really appreciate it!
The gist of your issue was the ordering of rewrite blocks. You usually put the more specific ones above the general rules. In your case:
# specific
RewriteRule ^images/video/(.*)/(.*).jpg /images/…
# generic
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
A better approach for Apache 2.4 and later is to use FallbackResource instead of the RewriteCond/Rule blob:
RewriteRule ^images/video/(.*)/(.*).jpg /image
FallbackResource index.php
Two more things:
<IfModule mod_rewrite.c>
</IfModule>
Is something that you should not commonly use. Conditional directives make sense in Apaches core configuration. For .htaccess RewriteRule blocks it's less advisable. It's unlikely that mod_rewrite randomly disengages at runtime. And if ever, you'd rather want HTTP 500 errors in your log instead of users seeing 404 Not Found results.
Also RewriteBase can shorten some rules, or abstract their residence. But you also shouldn't use it habitually. Instead just prefix blog/ to your match rules, and assemble all rewrites in the DOCUMENT_ROOT/.htaccess (or better yet VirtualHost section for performance).

Htaccess file extension in rewrite causing 404

I am having a problem with one of my rewrite rules. I am in the process of developing an application where I want to control file access via a script. My plan is to simply pass a rewrite rule through to my script with parameters to retrieve the appropriate file(s).
However, I am having some issues when trying to pass a file name with an extension as part of a rewrite rule. Perhaps someone can point out what i'm doing wrong.
My rewrite rule:
RewriteEngine On
RewriteBase /
RewriteRule ^files/(.+)/(.*)$ /index.php/files/$1/$2 [NC,L]
What works: http://localhost/files/12345/index
What is causing 404 errors to be thrown: http://localhost/files/12345/index.css
I've been battling with this for quite some time now, it almost acts like it's trying to load the css file, not finding it, and never running the rerwite rule. I am using IntelliJ's PHP Built-in Web Server for my host environment, not sure if that has anything to do with it or not.
UPDATE:
If I go to http://localhost/index.php/files/12345/index.css directly the page works as expected, it does not return a 404. This rule is the only rule in my .htaccess file.
If I echo the $_SERVER["PATH_INFO"]; from the index.php I get /files/12345/index.css back as the value
You need to skip existing files and directories from this rewrite:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(files/[^/]+/.*)$ /index.php/$1 [NC,L]
Found that the problem was simply that PHP Built-In Web Server doesn't honor .htaccess the same as apache does. Tossed this to a full blown apache server, and everything worked like a charm.

URL Rewrite giving 500 internal error

I'm trying to perform a URL Rewrite but giving me a 500 Internal Server Error
I have an index.php file that can take a parameter which I called cmd so the URL should look like:
http://localhost/some_folder/index.php?cmd=some_parameter
What I'm trying to achieve is allowing users to just type any of the following:
http://localhost/some_folder/some_parameter
OR
http://localhost/some_folder/index/some_parameter
Here is my .htaccess file:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^index.php/?$ index.php?cmd=shifts [NC,L]
I also tried:
Options +FollowSymLinks
RewriteEngine on
RewriteRule /cmd/(.*)/ index.php?cmd=$1
I don't know what I am doing wrong here!
I found this error Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration
You need to load mod_rewrite. See this answer for details for how to fix.
After lots of searching on the internet and especially stackoverflow, I reached a solution (not what I wanted, but it is working for now until I find another one that suits my needs better.
Here is the .htaccess that I'm using at the moment (actually I'm using 2 files as each folder/sub-folder got different parameters):
Options +FollowSymLinks
RewriteEngine on
RewriteBase /rootFolder # In the sub-folder I type /rootFolder/subFolder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule /cmd/(.*)/ index.php?R=$1 # In the sub-folder I type index.php?cmd=$1
Now I access using the URL http://localhost/rootFolder/index/R/xxx-1234 and http://localhost/rootFolder/admin/index/cmd/xxx
What I need to do is remove the index portion of the URL.

PHP Friendly Urls for my script

I have a site which I have converted to use cms made simple. It works perfectly and I have the friendly urls working fine too. My issue is with a little script I wrote myself and how best to integrate it. The script is a gallery script, it reads a directory and outputs a formatted gallery in html. I was planning on making it a user defined tag in cms made simple but I hit a small snag.
The gallery script needs to be able to read in two values from the url groupId and showpage.
If I am using freindly urls then the cms and use the tag I hit a snag as the cms tries to find an actual page at "www.mysite.com/gallery/mygroup/2" and then throws a 404.
basically I need
http://www.mysite.com/gallery/photogroup/2
rewritten to
http://www.mysite.com/gallery.php?groupId=photogroup&showpage=2
UPDATE
Follwoing Yuri's advice I added his rule to the htaccess. But I have hit another snag.
So for instance if we go to
http://www.mysite.com/gallery/photogroup/2
then Yuri's rule should take effect. But that path is also a correct physical directory on my site coincidentally. Is there a way to have the rewrite rule take effect instead of bringing me to a white screen browsing the files in the directory or to the forbidden screen if I have indexes turned off which I do.
Below is my htaccess
php_flag magic_quotes_gpc Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1
RewriteRule ^gallery/(\w+)/(\d+)$ gallery.php?groupId=$1&showpage=$2 [QSA,L]
So, did you try to write in .htaccess something like this?
RewriteEngine On
RewriteCond ${REQUEST_FILENAME} !-f
RewriteCond ${REQUEST_FILENAME} !-d
RewriteRule ^gallery/(\w+)/(\d+)$ gallery.php?groupId=$1&showpage=$2 [QSA,L]
sounds like a "module" to me. Maybe this Make your module use clean URLs

Categories