unextected redirection un xampp for windows - php

i'm creating a website, before uploading the site to web hosting i'm using XAMPP on windows. i was trying to use Search Engine Friendly URLs, for example:
http://localhost/mysite/[something]
to
http://localhost/mysite/index.php?p=[something]
i tried this .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([0-9A-Za-z]+) index.php?id=$1 [QSA,L] #and also tried http://localhost/mysite/index.php?p=$1
before this i tried more examples but they didn't work. The real problem is that for example if y type
localhost/mysite/some_page
i get an error (403), using the [R] flag i realized that redirection is: http://localhost/C:/XAMPP/htdocs/mysite/index.php?p=some_page
i removed htaccess file and restarted apache but problam persist with some urls

Perhaps I'm trying to solve a different problem, but if the [something] always leads to content served by your index.php, then just send all requests to index.php and let that page use the URL string to get the variable you need to serve the right content (ie the string after the /).
You may need to turn off MultiViews in your .htaccess using Options -MultiViews.
The following is what you need in your .htaccess
RewriteBase /
# Where "/" is the location of your index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA, L]
Hope this helps.

Related

Rewrite/hiding some URL in .htaccess

I'm trying to get right syntax for .htaccess without any result...
I've a URL structured as domain.com/app/public/pageName .
It's working fine but I would "hide" the 'app/public/' part in browsers, basically doing something like:
[real URL] domain.com/app/public/pageName -> domain.com/pageName [what users type and see in browsers]
I think in that way it should be more readable and seo-friendly.
As I understood from docs (and maybe it's wrong because it's not working...) I should tell to Apache to map/redirect all URL like domain.com/pageName to domain.com/app/public/pageName , but only internally, in order to show the minimal URL in users' browsers.
Right now I have something like:
RewriteEngine on
#RewriteBase /app/public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ https://localhost/app/public/index.php?url=$1 [QSA,L]
(I'm using full URL with https://... in order to get something that will be quick and easy to adapt when I upload all to my hosting, is it right?).
Problem is that RewriteRule actually change the URL, because it perform a redirect and URL rewrite it's not handle internally.
So, first of all: is it possible what I'm trying to do? If so, how can I handle the URL rewrite only internally?
Everything should be uploaded to a shared hosting, so I don't have other than .htaccess.
Anyway, I can consider to upgrade to a vps if there are not other possibilities...
Thanks!
==============
EDIT (should be more clear now)
tl;dr version:
I'm looking for a method that let users to type domain.com/pageName (and they will see that address in their browsers) and rewrite internally that URL in order to point to domain.com/app/public/pageName.
==============
More: after /app/public/ there can be an arbitrary number of elements, separated by / . All of these elements are appended at the end of the URL after index.php. At the end URL looks like:
domain/app/public/index.php?url=lot/of/elements/here
This is already working with the RewriteRule posted above, I would keep that too.
Thanks!
This is working fine for me, Hope it will work for you as well.
Check .htaccess here
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/app/public
RewriteRule ^app/public/(.*)$ /$1 [L,QSA]
Just for reference, I found a solution, maybe will be usefull for someone.
Basically I moved .htaccess to the root server (instead of /app/public directory) and changed the RewriteRule as follow:
RewriteEngine on
RewriteBase /app/public/
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [PT]
Now it's working (at least on localhost).
What do you think? Are there any side effects with this config?

Changing the actual URL without changing display URL using htaccess [duplicate]

So, I've this problem:
Base Website located at http://example.com/
Second Website located at http://example.com/web2/
People making various requests to the second website like
http://example.com/myWeb/pg1 and http://example.com/web2/pg2
Recently and due to some other issues I need to have a custom new path for the second website but also keep the first one working.
The ideia is to allow users to access the second website over the two following addresses:
http://example.com/web2/
http://example.com/alternative-url-web2/
The folder /web2/ actually exists on the server, but how can I simulate the folder /alternative-url-web2/ and "redirect" the requests to /web2/?
Please note I don't want the URL on the browser to change, this must be a "silent redirect". And I also make sure that all other requests like http://example.com/other are not redirected by the second website.
Thank you.
Update:
According to #anubhava I could simply solve this issue by adding in my .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^alternative-url-web2(/.*|)$ /web2$1 [L,NC]
This is probably working fine but I noticed the following:
http://ex.com/alternative-url-web2 is redirected to http://ex.com/web2/ (changing browser URL);
http://ex.com/alternative-url-web2/ is redirected to http://ex.com/(changing browser URL);
http://ex.com/alternative-url-web2/someRequest works fine and does NOT change the browser URL;
http://ex.com/alternative-url-web2/index.php works fine and does NOT change the browser URL;
Site Note:
At /web2/ there's an .htaccess that might be cause the wired redirect behavior above... So here is the file contents:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</IfModule>
Can the internal RewriteRule to index.php be causing all this? If yes, how can I fix it?
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^alternative-url-web2(/.*|)$ /web2$1 [L,NC]
Alternate code:
RewriteRule ^alternative-url-web2/?$ /web2/ [L,NC]
RewriteRule ^alternative-url-web2/(.+)$ /web2/$1 [L,NC]
This is a pretty simple rewrite. In the htaccess file in your document root, just add the following:
RewriteEngine On
RewriteRule ^alternative-url-web2/?(.*)$ /web2/$1 [L]
Unlike a redirect, which makes the browser/client send a new request for a new URL (thus changing what's in the browser's location bar), a rewrite happens entirely on the server's side.
By the way, in order to follow the trail of htaccess redirects, you could add something like this to each of them:
Header add X-Remark-Rewrite "/path.to/htaccess"
You can inspect these in the response in the developer tools.

htaccess Redirect Causes Errors

I'm working on a website that has been built sloppily.
The website is filled with regular links that are translated into the corresponding .php pages by the .htaccess page.
This is it:
RewriteEngine on
RewriteRule ^koral/(.*)/$ page.php?name=$1
RewriteRule ^koral/(.*)$ page.php?name=$1
RewriteRule ^(.*).html/(.*)/(.*)/(.*)$ cat.php?cat=$1&page=$2&order=$3&dir=$4
RewriteRule ^(.*).html$ cat.php?cat=$1
RewriteRule ^(.*)/(.*).html$ product.php?cat=$1&product=$2
<IfModule mod_security.c>
SecFilterEngine Off
</IfModule>
First of all, I would love some help regarding whether or not this page has everything it should. I've never messed with it before.
Secondly and my main issue, if, for example, I would write the address www.thewebsite.com/foobar.html, it would be translated into www.thewebsite.com/cat.php?cat=foobar by the .htaccess page, and it would give a database error (and reveal information about the database).
I've put a check into cat.php which checks if the category exists, but I can't redirect the user to the 404 error page. There's a page called 404.shtml in the website, but redirecting the user to it causes the .htaccess to just change it again to cat.php?cat=404.
Is the way they used the .htaccess page normal? Should I change this system?
And how are users sent to error pages? From what I understood the server should be doing it on its own?
I would love some clarification... There is some much about this subject I don't understand.
Update:
This is my new .htaccess page
RewriteEngine on
RewriteRule ^error.php?err=(.*)$ Error$1.html
# Only apply this rule if we're not requesting a file...
RewriteCond %{REQUEST_FILENAME} !-f [NC]
# ...and if we're not requesting a directory.
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^koral/(.*)/$ page.php?name=$1
RewriteRule ^koral/(.*)$ page.php?name=$1
RewriteRule ^(.*).html/(.*)/(.*)/(.*)$ cat.php?cat=$1&page=$2&order=$3&dir=$4
RewriteRule ^(.*).html$ cat.php?cat=$1
RewriteRule ^(.*)/(.*).html$ product.php?cat=$1&product=$2
<IfModule mod_security.c>
SecFilterEngine Off
</IfModule>
Because the redirecting is in the code and the user cannot see it, I allowed myself to write the link in a non-clean way. I tried turning it into a clean URL but the following does not do anything:
RewriteRule ^error.php?err=(.*)$ Error$1.html
Can someone please help me understand why? I thought since error.php is a real page, I should put it before the conditional but it didn't work. BTW, I saw in an article about .htaccess that the page should start with Options +FollowSymLinks. It seems to me that everyone sort of has their own way of writing it. Is there a guide or something like that, which I can be sure is authentic and covers all the bases there is about .htaccess?
Thank you so much!!
Using rewrite rules to work around links to .html pages that don't exist is unusual in my experience, but it's really just a different take on "pretty" URLs, e.g. www.thewebsite.com/foobar/ gets routed to cat.php?cat=foobar on the backend.
Your 404 issue is different. You need to be able to display error pages.
One option here is to rewrite requests as long as they don't request an existing file. This is very common for serving up static content like images, CSS files, and the like. To do this, you can use the -d and -f options to RewriteCond, which apply when requesting a directory and file respectively:
RewriteEngine On
# Only apply this rule if we're not requesting a file...
RewriteCond %{REQUEST_FILENAME} !-f [NC]
# ...and if we're not requesting a directory.
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^([^.]+)\.html$ cat.php?cat=$1 [L,QSA]
Now, requests to 404.shtml should go through, because you're requesting an existing file on the filesystem.
Note that the RewriteConds only apply to the single RewriteRule that immediately follows. For additional RewriteRules, also include additional RewriteConds.
Your regex is wrong anywhere. Literal dot needs to be escaped using otherwise it will match any character. Also it is better to use L and QSA flags to end each rule properly.
RewriteEngine on
RewriteBase /
RewriteRule ^koral/([^/]+)/?$ page.php?name=$1 [L,QSA]
RewriteRule ^([^.]+)\.html/([^/]+)/([^/]+)/([^/]*)/?$ cat.php?cat=$1&page=$2&order=$3&dir=$4 [L,QSA]
RewriteRule ^([^.]+)\.html$ cat.php?cat=$1 [L,QSA]
RewriteRule ^([^/]+)/([^.]+)\.html$ product.php?cat=$1&product=$2 [L,QSA]

Strange situation mod_rewrite and PHP

I do not understand something ...
I have CMS written by me and have some strange problem about hidden error from "mod_rewrite". In apache2 error.log "mod_rewrite" giving :
File does not exist: /home/path/to/request, referer: http://IP/request?view=1
my $_GET / $_POST request are all empty
$_SERVER['REQUEST_URI'] is ok (returns request?view=1)
$_SERVER['QUERY_STRING'] is also empty
also i install Drupal CMS to test is it problem on web server, but Drupal works just fine... then i copied drupal ".htaccess" file to my CMS in order to fix mod_rewrite preferences. (Drupal do not create apache error like my )
No luck, i have same problem again.
also tried with all options in .htaccess from Drupal CMS
I try to turn on RewriteLog (several attempts) but without result, not a single log file was not created.
Am i something missing or ... just my cms sucks
and one more thing, my CMS work just fine on other webserver with exactly the same files .... ?????
RewriteEngine On
Options -Indexes
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
#tested RewriteRule ^ index.php [L]
#tested RewriteRule ^ index.php [L,QSA]
#tested Options FollowSymLinks
#tested Options -MultiViews
#tested RewriteBase /
P.S. sorry for my bad English
Not sure if that's your whole .htaccess but you need to enable rewrite, here's a rewrite I use and its never failed:
RewriteEngine On
Options -Indexes
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]
The RewriteRule passes everything (.*) to a route parameter in index.php then my router class handles the controller ect, the QSA flag will also allow me to pass extra GET/POST values to any part of the script.
hope it helps
ps I dont add the RewriteCond %{REQUEST_FILENAME} !-d because I dont allow folder views and I dont serve content from a folder, everything goes through a controller inc (images,css,js), so there is no need for this in most cases. And it also protects the folders like core & template ect ;)
finally I found the solution of my problem!
The answer is very,very,very simple...
In apache2 configuration (on Ubuntu) I found the <Directory tag is duplicated by default ... I just erase all directories tags and create one basic.
reload apache2 configuration and every thing starts working just fine

.htaccess issues: No input file specified

Can someone help me with this? I'm feeling like I've been hitting my head against a wall for over 2 hrs now.
I've got Apache 2.2.8 + PHP 5.2.6 installed on my machine and the .htaccess with the code below works fine, no errors.
RewriteEngine on
RewriteCond $1 !^(index\.php|css|gfx|js|swf|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]
The same code on my hosting provider server gives me a 404 error code and outputs only: No input file specified. index.php is there. I know they have Apache installed (cannot find version info anywhere) and they're running PHP v5.2.8.
I'm on Windows XP 64-bit, they're running some Linux with PHP in CGI/FastCGI mode. Can anyone suggest what could be the problem?
PS. if that's important that's for CodeIgniter to work with friendly URLs.
Update1:
mod_rewrite is installed and on.
What I've noticed is that if I change in RewriteRule to /index.php?$1 (question mark instead of forward slash) it goes into an infinite loop. Anyway, using question mark isn't an option as CodeIgniter (required) is not going to work this way.
Homepage also works when I request index.php directly: example.com/index.php
I'm starting to think it might be apache thinking that once the trailing slash is added it is not a file anymore but a folder. how to change such a behaviour?
Update 2:
I was wrong.
Apache handles these URLs correctly.
Requesting http://example.com/index.php/start/ (homepage) or any other valid address works.
Seems that Apache is just not forwarding the query for some reason.
Update 3:
Just to be clear what I'm trying to achieve.
I want to rewrite addresses like that:
http://www.example.com/something/ => http://www.example.com/index.php/something/
http://www.example.com/something/else/ => http://www.example.com/index.php/something/else/
I was beating my head up against this as well. I'm also installing Code Igniter.
The goocher was no RewriteBase. Here's my .htaccess:
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
The Problem
I encountered a similar problem just now and unfortunately none of the answers in this thread helped:
Zend Framework was giving out "No input file specified.", but:
The default RewriteBase was just fine, and adding RewriteBase / did not help
It's a shared hosting server and only FastCGI is available (no ability to switch to SuPHP)
AcceptPathInfo was on
There was no problem with URL rewriting in general on the server
So the answer came from the following site:
https://ellislab.com/forums/viewthread/55620/P15 [dead link]
(even though the host is not DreamHost).
The Solution
Apparently all you need to do is replace this line:
RewriteRule ^(.*)$ index.php/$1
With this:
RewriteRule ^(.*)$ index.php?/$1
Problem solved.
This worked for me:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
After index.php, the question mark is important!
Try if it works with a simpler RewriteCond; like one that rewrites only everything that isn't an existing file/folder/link:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /index.php/$1 [R,L]
Go Daddy Users:
login to your Go Daddy Account
click on your hosting account.
go to Settings > File Extensions Management
change .php and .php5 to run under PHP5.2X (instead of PHP5.2xFastCGI)
SOLVED!!!!
mod_rewrite is a bit too smart for its own good, because it tries to figure out what sort of redirect it should be doing. In this case it looks to mod_rewrite like you're trying to redirect to a folder, so it looks for the folder and can't find it, hence the error.
Edit: Just to be perfectly clear I think your best bet is to change your rewrite rule to:
RewriteRule ^(.*)$ /index.php?$1 [L]
unless there is a very speciic reason why you want it to be a forward slash.
Edit 2: I see that you already tried this. The reason you're getting an infinite loop is because you have index.php in your rewrite condition. If you remove that you should be free of the infinite loop.
this code will fixed this issue.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
.htaccess for Live Server :-
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
.htaccess for Localhost :-
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php/$1
It is very likely that the administrator of your host has disabled the ability to use Rewrite in .htaccess. They might not even have mod_rewrite installed.
Drop them an email and ask
Since this is a server configuration issue, perhaps you should ask at Server Fault
Edit (since you are sure that the server is configured correctly)
Have you considered tagging your RewriteCond with an end of line $?
RewriteCond $1 !^(index\.php|css|gfx|js|swf|robots\.txt|favicon\.ico)
Will (based on my limited knowledge) block any url that contains index.php, css, gfx ... at the start of a url. Because you don't have a $ at the end of the regexp, it will also block any urls that continue on from there...
I.e www.yourdomain.com/index.php/something is not redirected, same with www.yourdomain.com/js/something
Perhaps you want to add a $, which will require the url to end immediately after your regexp.
RewriteCond $1 !^(index\.php|css|gfx|js|swf|robots\.txt|favicon\.ico)$
Here is one time I caught no input file specified right on action:
This causes it:
RewriteRule ^(.*\.swf)$ redirect_php.php/?a=1 [last]
This corrected it:
RewriteRule ^(.*\.swf)$ redirect_php.php?a=1 [last]
note the / before query ?
This seems really related to AcceptPathInfo, which is about the ability to read paths after file names:
http://domain.com/file.php/tricky_path/?regular_query_stuff
Since this question seems to attract a lot of attention I'd like to propose another answer for people having encountering the same problem and are unable to solve it with the help of the existing answers. I myself was one of those people until five minutes ago.
Always, I mean always check your server logs because they might present useful information to you.
After checking my server logs (Apache2.4) I found out that open_basedir caused the trouble:
mod_fcgid: stderr: PHP Warning: Unknown: open_basedir restriction in effect. File(/data/sites/domain/public/index.php) is not within the allowed path(s): (/usr/local/lib/php:/usr/local/bin:/data/sites/domain/http-docs) in Unknown on line 0
mod_fcgid: stderr: PHP Warning: Unknown: failed to open stream: Operation not permitted in Unknown on line 0
In this case, open_basedir could not handle a symbolic link I created because it points to the outside of the open_basedir settings. Either broaden the open_basedir setting to also the new location or move the required files to the inside of any allowed directory..
You may be using Nginx, not an Apache. The error message will be the same.
echo out your sever data to be sure.
echo $_SERVER["SERVER_SOFTWARE"];
I spent hours trying all recipes from SO until I found the solution: you have to add question mark (?) after ".php", so last line of your rewrite rules will look like:
RewriteRule ^(.*)$ index.php ? /$1 [L]
There was no ? In my CodeIgniter setup from previous server, cause it usedpure Apache (no Nginx). And no recipes with port forwarding, nginx reconfiguration or php-fm reinstallation helped -- I tried them all on my VDS.
That simple method solved all in seconds.
In my case, the rewrite engine was conflicting with the doc_root directive in php.ini. The rewrite engine was treating the rewritten URL as a local file path and prefixing it with the document root, only to be prefixed again by PHP.
The solution was to rewrite to a relative URL, and add the PT flag. This tells mod_rewrite to pass the result to normal URL processing.
RewriteRule "^/(unwanted-part)/(.*)$" /$2 [PT]
In my case I am running laragon it's happening due to php.ini file, then I removed the php and install it again and it worked successfully. I think made some changes in php.ini file that's why it's displaying no input specific. After installing php.ini it fixed my issue.
update php.ini
Maybe your server has AcceptPathInfo disabled that is essential for that kind of URL to work properly. Try to enable it:
AcceptPathInfo On
Ok, try this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^index\.php(/|$) index.php%{REQUEST_URI} [L]

Categories