mod_rewrite bug and permission denied - php

i'm using mod_rewrite to rewrite sites like "url.com/foo" to "url.com/index.php?site=foo".
The Code:
RewriteEngine On
RewriteBase /
RewriteRule ^([0-9a-zA-Z]+)$ index.php?site=$1
If i click on a link with href="/foo" it will give me
http://url.com/foo/?site=foo
The code works at other sites on the same V-Server, i have Boilerplate installed, BUT i dont use the .htaccess of it right now to fix the mod_rewrite, so it cant be a boilerplate issue, or?
I also tried the following code:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ index.php?site=$1
That code, at least, rewrites to index.php?site=foo
BUT it still creates url's like the upper example with the unnecessary folder between.
Edit: to make sure everyone understands the overall idea:
The files are based in http://url.com (index.php, .htaccess) , so i set the RewriteBase to "/".
It should take links like
http://url.com/foo
and rewrite them to
http://url.com/index.php?site=foo
there are no folders included.

I solved it, damn i'm so stupid ;)
I have a folder called url.com/foo/ and the link im redirecting is url.com/foo to url.com/index.php?site=foo so it is always trying to get into that folder. Since i renamed the folder it is working very well.

I have been getting a similar error message on RewriteRule
pcfg_openfile: unable to check htaccess file, ensure it is readable
I also have the subfolder name similar to the redirected URL part but renaming it did not solve the problem (as suggested by Chris).
After multiple attempts, the problem was solved by updating the subfolder permissions to 755 (using cPanel folder permissions dialog) :
User: Read, Write, Execute
Group: Read, Execute
World: Read, Execute
PS: I am not sure if renaming the subfolder was actually required for this scenario since before and after the rename, error message was same.

Related

.htaccess Completely rewrite URL to show file in a folder, rather than page in root

So, I have the current file structure:
ROOT
-> /public
-> /user_views
user_handle.php
user_profile.php
user_feed.php
user_settings.php
.htaccess
As you see, the folder user_views contains a few of the possible views that the client could want to look at. What I am wanting, is for clients that insert the URL http://example.com/user/ to be directed to the page user_handle.php. This handle would act as a root file for all /user/ pages, and it would accordingly split into those pages through numerous $_GET requests.
So far, I have the following .htaccess, but it's not working...
RewriteRule ^user/ user_views/user_handle.php [L]
What could I do to get this to work, so that the url http://example.com/user redirects to the user_handle file in the user_views folder?
Thanks!
I'm not sure I fully understand your question, but it seems you would like to make user_handle.php located under public/user_views act as a "router" for the rest of you PHP files and have all requests to /user/ (e.g. /user/?page=1) be processed by user_handle.php.
If that's the case, your rule seems legit. The only thing I noticed (I might be wrong) is that your .htaccess is located outside the public folder. In that's the case, you need to include 'public/' as part of your rule.
I recreated the folder/file structure you described and it has worked for me using the following .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^user/ public/user_views/user_handle.php [L]
</IfModule>
Slight chance this is the problem, but you also might want to double check that mod_rewrite, which is the rule-based rewriting engine is enabled on your server/local environment. It should show up under 'Loaded modules' when you call phpinfo() in any PHP file.
Hope this helps.

Apache rewrite rule for a whole directory

I am trying to rewrite a directory to its own sub folder. I have an .htaccess file in the directory, and I also put the AllowOverride All in the Apache conf file.
Basically, I want the server to redirect this url: http://example.com/MPOS to http://example.com/MPOS/public. The closest I got was using this:
RewriteRule /MPOS$ /MPOS/public/ [L]
It does the job, going to http://example.com/MPOS/public, but its the rest of the resources (all the pages, stylesheets and the rest) are not being redirected, and so I get the page as if the links are broken. Inside public folder there is "index.php" file.
I know there are many questions about it, but after hours of search nothing helped me, so I posted this question.
Any help would be really appreciated.
Thanks,
Shay
OK, I feel a bit stupid. Managed to get it to work by writing the rewrite like this:
RewriteRule ^$ /MPOS/public/ [L,R]
When the .htaccess is found in the "MPOS" folder.

File not found with .htaccess file on Apache server

I have this .htaccess on my Windows dev machine and everything works fine
RewriteEngine On
RewriteRule ^(.+[^/])/$ http://%{HTTP_HOST}/webserv/$1 [R=301,L]
RewriteRule ^login/([0-9]+)/([0-9]+)$ login.php?par1=$1&par2=$2 [L,NC,QSA]
When I upload it on my Centos server I get the error
404 The requested URL path_to_script/login.php was not found on this server.
I cannot understand why this is. The file is definitely there and I gave permissions 777.
I have checked if the .htaccess is actually used by adding rubbish in it and getting an internal error (500) which means that it is working.
Also when I use .htaccess to make restrictions on IPs that have access to the directory it works. mod_rewrite is loaded as shown in the phpinfo() I have tried.
I am at a loss here.
EDIT
I noticed that it tries to search in the base_path appending it to the root path e.g.
/path_to_script/path_to_script/login.php
And that is why it fails. How do I solve this?
I finally solved it but in a way that I am not sure I like. At least it works. If someone has a better solution I would be glad to see it.
RewriteRule ^login/([0-9]+)/([0-9]+)$ http://host/webserv/login.php?par1=$1&par2=$2
The reason I don't like this solution is because I does not give a pretty url. It just redirects.
I still do not understand why I get the root directory appended to the request if I instead write:
RewriteRule ^login/([0-9]+)/([0-9]+)$ login.php?par1=$1&par2=$2

Forbidden error coming on delete image on main server,localhost:its working fine

I have developed a website.On 1 page there is a picture gallery,images are getting uploaded perfectly but as soon as i delete an image,this error comes:
*Forbidden
You don't have permission to access /fdccms/backend/project_categories/view_image.php on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.*
Deleting an image is working perfectly on localhost but i don't know the problem with server.
In .htaccess also i have included rewriterule:
RewriteRule ^view_image/([^/]*)/?(.*)$ http://fdc-int.com/fdccms/backend/project_categories/view_image.php?id=$1&del=$2
Check your folder permissions. Make sure you have read, write and execute permissions for that folder. (that would be 777)
if it's your .htaccess, try this:
RewriteRule ^view_image/([^/]*)/?(.*) /backend/project_categories/view_image.php?id=$1&del=$2 [L]
since you said fdccms is your webroot.
either way the $ is un-needed since the . matches any character.
I guess you have some trouble with your rewriteRule.
Probably ?(.*) does not work as expected. The QUERY_STRING should be added automatically, but sometimes that does not happen. I am not sure, if it does not work, if there is a ? in the Rewrite Target.
so try this:
RewriteRule ^view_image/([^/]*)/ /backend/project_categories/view_image.php?id=$1&del=%{QUERY_STRING} [L]

Is PHP allowed to modify .htaccess file in current folder?

I have a PHP web app located on shared hosting.
My goal is to modify .htaccess file from PHP code when the PHP page is running.
I need that .htaccess to insert a couple of mod_rewrite lines into it.
The problem is that on Windows+Apache I can dynamically modify .htaccess file
but the same code on Linux reports a problem when I try to access this file in any
way (copy or fopen):
"failed to open stream: Permission denied"
I have given .htaccess file 777 permissions - still no result.
WHat prevents me from doing this? How can I develop a workaround?
P.S.
My initial goal was to be able to add a new RewriteRule into .htaccess that maps a
newly added category_id with new category_name.
If it wasn't shared hosting, I would use something like RewriteMap (in main Apache config) and would be able to access the map file.
This is the first real limitation I've been unable to tackle with PHP+Apache, but I hope it's circuventable too.
This seems like an overly-complex solution to just having a general "load category" page that takes the category name from the URL and loads the corresponding ID.
For example, if the URL is:
http://yoursite.com/category/programming
I would remap that to something like:
http://yoursite.com/category.php?name=programming
I want to suggest something else that also works. Instead of writing a rule for every 'special' url, why not use one for all?
I found it a whole lot easier to use what wordpress uses: every url is redirected to the index.
All you have to do is, set up the index file, read in the directory that was loaded (perhaps using $_SERVER['URI_REQUEST']), and deal with it.
add to .htaccess this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Thanks to that chunck you have a system somewhat unlimited at your disposal. If you ever feel like renaming you categrory url, or add another special case, it's already ready!
You only need a small set of rewrite rules. To do what Chad suggests:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/category/.*$ category.php [QSA]
Thus, anytime someone navigates to /category/category_id, the request will be directed to category.php, which will be handed the /category/ URI in $_SERVER['REQUEST_URI'], from which you can easily get the category ID, and you don't need to bother with editing the .htaccess file every time you add a category.
While I agree with the above comments, it can definitely be done. PHP apps like WordPress do exactly this based on changes made on the settings page. It should be as simple as writing the file out however the parent directory NEEDS to have permission for the web server user to write to it.
If it isn't working for you the trick will be making the parent directory either 777 or 775 and having the group set to whatever group Apache runs under (usually "apache" or "www" or something similar)
Adam (commented on your question) is quite correct though, some other security layer on your server might be preventing you from doing this, and this is probably a good indication that you might be approaching the problem the wrong way.
I agree with Chad Birch. In case you can't be dissuaded, though, in your situation I would first be looking for parent directories with locked-down permissions.
FYI, one of the reasons that rewriting the .htaccess is a bad idea is that any requests that come in while the .htaccess is being rewritten will not have any of your redirects applied.

Categories