HTAccess Rewrite affecting other files in the Root? - php

I'm on a 1and1 shared server containing my other websites.
In the Root I have an HTAccess file Rewriting a photo viewing php file with an ID as a parameter.
RewriteEngine On
RewriteRule ^([^/d]+)\.html?$ /photography/albums/viewphoto.php?id=$1 [QSA,NC,L]
Example:
Current URL:
http://www.example.com/photography/albums/viewphoto.php?id=C01-A01-P01
Desired URL:
http://www.example.com/C01-A01-P01.html
All works for the rewrite of the URLs, but the html files located in the root have stopped including my PHP code functions and variables held in an .inc file.
Am I missing something in making the rewrite specific to a single file or is an option missing on the rewrite?

You need to use a RewriteCond to exclude existing files from the Rewrite ,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)\.html?$ /photography/albums/viewphoto.php?id=$1 [QSA,NC,L]

Related

.htaccess spits our full URL rewritten URL on folder name

I have the following .htaccess:
RewriteEngine On
RewriteBase /sandbox/
RewriteRule ^(assets)($|/) - [L]
RewriteRule ^(.*)$ lib/script/bootstrap.php?route=$1.php [NC,L,QSA]
What I am doing is redirecting all requests (except for my assets - img, css, js) to my bootstrap script, which will handle the request via the route parameter, which includes the URL request.
This works fine in most cases, except for when I have a call such as:
http://www.example.com/sandbox/admin, where admin is a folder that exists in my directory, amongst other folders. This folder contains various pages that are accessible, aswell as an index.php file that my bootstrap.php routing will redirect requests from the above URL to it, by default.
My problem is that when I attempt to hit the page: http://www.example.com/sandbox/admin, my htaccess (as far as I know) is rewriting the visible URL to http://www.example.com/sandbox/admin/?route=admin.php, which is not as pretty. I believe it's because there's some conflict going on with the fact that the above URL is a valid URL pointing to my /admin/ folder, but I would like my .htaccess to ignore this and send this aswell to my bootstrap.php (line 4 of my .htaccess). What am I doing wrong?
Here is the requests coming in my browser:
UPDATE:
Strangely, http://www.example.com/sandbox/admin/ works fine, but not http://www.example.com/sandbox/admin (notice the trailing slash).
If you have real folders you need to use a condition to ignore real files and folders. All other requests will get routed to your bootstrap file. (If I understand your issue correctly.)
RewriteEngine On
RewriteBase /sandbox/
RewriteRule ^(assets)($|/) - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ lib/script/bootstrap.php?route=$1.php [NC,L,QSA]

Rewrite everything after a certain directory to that directories index.php (htaccess)

Hi, so this may be asked elsewhere but I have searched and come up with irrelevant results.
I clearly don't know what to search for exactly.
I'm just trying to rewrite everything after a certain directory to that directories index.php.
Here is an example of the URL a visitor would SEE
website.com/search/location/United%20States
And I would like that URL to be rewritten server-side so that it loads website.com/search/location/index.php
(not a 301 redirect)
I would like the Url to stay the same but load the index.php script (to include United%20States so this can be passed to PHP to determine what the location is and if it is legitimate etc.).
Sorry I know that this will be somewhere already but I just can't find it
I have some code already but it is buggy and seems to choose when it wants to work and also sometimes uses location/index.php/United%20States which is not what I want.
Put this code in your htaccess (which has to be in your root folder)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search/location/.+$ /search/location/index.php [L]
If you have Apache web server, make sure you have mod-rewrite enabled and put .htaccess file into your WEBROOT/search/location directory. Put this into .htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php [L]
This will internally redirect all requests, where file or directory does not exists, to index.php.
You could also put .htaccess file into your WEBROOT directory and write this into to:
RewriteRule ^search/location/.* /search/location/index.php
Hope this helps.

How can I load my PHP file for any HTTP requests inside a directory?

I have a proxy I've been writing with PHP for a while.
Right now, requesting a url like www.mysite.com/proxy/?folder/page.html on my server will return the page at www.theirsite.com/folder/page.html.
I basically am having my index.php file use everything past /proxy/? as the request URI for www.theirsite.com. Any images are copied to the folder /proxy/images/ and the src attributes of the <img> tags are changed accordingly. All this is working great.
Now I would like to change my script so that I will not need the ? anymore. However, the url www.mysite.com/proxy/folder/page.html would result in an HTTP request to page.html, which doesn't exist on my server.
This isn't what I want. I need index.php to be loaded instead, so it can return the page at www.theirsite.com/folder/page.html. To accomplish this, I imagine I would need to use Apache's mod_rewrite, which is working with my WordPress installation.
What would I need in my .htaccess file to do this correctly, while still allowing access to files that exist in the /proxy/images/ directory? Would this affect $_SERVER['REQUEST_URI'] at all?
RewriteEngine on
RewriteRule ^/$ /index.php [R]
Well, I got it working how I wanted. Here's the full contents of my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /proxy/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /proxy/index.php [L]
</IfModule>
Now for the url www.mysite.com/proxy/folder/page.html, index.php is loaded, and $_SERVER['REQUEST_URI'] returns /proxy/folder/page.html. Files that actually exist in the /proxy/ folder or any subfolders do not rewrite to index.php, which is what I wanted.

Normal php in a Cake PHP app and using htaccess rewrite

I have a normal php script which I want to use in a CakePHP application (created by a previous developer). I have made a folder in my CakePHP webroot folder:
Httpdocs -> app -> webroot -> folderx
Is it possible to run a standalone script in this folder?
Also, my script takes in an id, for example www.domain.com/folderx/index.php?id=123 and draws the information (ex name of the product) from the DB. However, I wish to rewrite the URL to:
www.domain.com/folderx/name-of-the-product. Is this possible at all?
The webroot already has an .htaccess file which handles the CakePHP rewrites. Would I be able to use a new .htaccess file with the rewrite instructions in www.domain.com/folderx/ for my standalone script.
You need to modify the existing rewrite rules to not rewrite requests that include /folderx/ to CakePHP. If you need help with that and the mod_rewrite manual isn't enough, share your rewrite rules.
By default, your htaccess, in webroot, must contain:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
In this case, all the folders and files in the webroot should open normally

How to use rewriterule to redirect to a php file in the same folder?

I would like to take requests for /somefolder/style.css and handle them with /somefolder/program.php
So, I put the following in my .htaccess file:
rewriteengine on
rewriterule ^style.css$ program.php?css=1 [R=302,L]
The result is that instead of redirecting to /somefolder/program.php, the server tries to redirect to:
/var/www/html/somefolder/program.php?css=1
How can I get rid of the /var/www/html/ in the redirect? I thought that since I just entered program.php in the .htaccess that it would default to the same folder.
Since this is a generic script that I will use in many places, I would like to avoid using rewritebase to specify the folder I'm in -- the .htaccess has to work in any folder without being modified.
Leave the R flag away and you will get an internal redirect:
RewriteRule ^style\.css$ program.php?css=1 [L]
Otherwise specify the full URL path you want to redirect to externally:
RewriteRule ^style\.css$ /program.php?css=1 [R=302,L]
Or for any arbitrary folder:
RewriteCond %{REQUEST_URI} (.*)/style\.css$
RewriteRule ^style\.css$ %1/program.php?css=1 [R=302,L]
I think the problem is that you are missing a ReWrite base statement.
Also the I would put the .htaccess file in the root directory of your site. That way you don't have to copy an .htacess file into every new directory you create. What if you want to change the name of your php file? You'd have to change every .htaccess file.
This is how I would redirect www.mydomain.com/orange/style.css to www.mydomain.com/orange/program.php?css=1 using generic rules:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/style\.css$ $1/program.php?css=1 [L]

Categories