How can I get mod_rewrite to rewrite everything? - php

What I've been trying to achieve is to get my .htaccess file to rewrite ALL URLS.
No matter what I do, however, I cannot get it ignore existing directories. And by that, I mean, act as if they don't exist.
For example, say I have the following file structure:
/
dir1
file1.php
dir2
file2.php
.htaccess
And suppose I want to redirect all traffic to dir1/file1.php?url=path.
This never works for me if the path is an existing directory.
For example, if I navigate to url/path/stuff/dir2, the "redirecting" works, but the URL in the address bar changes to url/path/stuff/dir2/?url=dir2 for some unfathomable reason.
Here is my .htaccess:
Options -MultiViews
Options -Indexes
Options +FollowSymLinks
# so navigating to a url with a trailing slash won't cause problems
DirectorySlash Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dir1/file1.php?url=$1 [QSA,L]

You (updated) .htaccess is saying: IF (it is not an existing file) THEN { rewrite the URL }. Seems like you want
RewriteEngine on
RewriteCond %{REQUEST_URI} !dir1/file1.php
RewriteRule ^(.*)$ dir1/file1.php?url=$1 [QSA,L]
In this case, the RewriteCond just prevents infinite looping. The RewriteRule gets applied once and only once. Rewrite module will keep cycling thru all the rules until a cycle does not result in a changed to the URI.

Related

RewriteRule not working with the given parameter

I did some research and spent my last 2 days trying to make my .htaccess work without success, and I can't fully understand how .htaccess really work.
This is the URL I'm trying to rewrite, using GET in my .php files:
http://localhost/BDsite/tables/table.php?table=Energy
and I want it to be like this:
http://localhost/BDsite/tables/Energy
Well, this is how my .htaccess is written, and its located inside the site folder, which is /BDsite
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^BDsite/tables/([^/]*)$ /BDsite/tables/table.php?table=$1 [L]
</IfModule>
Sadly nothing is happening with my URL.
As opposed to per-server rewrites, it is possible to do rewriting inside sections or .htaccess files at the expense of some additional complexity. This technique is called per-directory rewrites.
The main difference with per-server rewrites is that the path prefix of the directory containing the .htaccess file is stripped before matching in the RewriteRule.
A RewriteBase should be used to assure the request is properly mapped.
(source: apache.org - rewrite intro .htaccess files)
So, using your directory structure, and adding the necessary RewriteBase you'd get the following that should work:
.htaccess file in root folder (BDsite)
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /BDsite/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^tables/([^/]*)$ tables/table.php?table=$1 [L]
</IfModule>
Project structure:
So now if you request: localhost/BDsite/tables/energy,
you'll get served localhost/BDsite/tables/table.php?table=energy
(check this by var_dump-ing $_GET in file table.php)

passing every url through a routing file in php using .htaccess

/
.. .htaccess
.. index.php
I have two files shown above in my servers root directory. I want to write a rule in .htaccess so that every time it will pass every parameters (i mean (.*)) to index.php?controller=parameter and index.php will route it accordingly. How can I write such a rule. Thanks
This rewrites everything to index.php except for existing directories/files:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /pro/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?controller=$1 [L,QSA]
Passing url to controller is not mandatory, since you can use $_SERVER['REQUEST_URI'] (for example) in your index.php

Sending specific folder to HTTPS and the rest from non-www to www, while keeping the rest of the laravel routes working?

I am trying to write a .htacess file such that:
The site runs php 5.4
Requests to domain.com run index.php first
Requests to http://domain.com/checkout are redirected to https://domain.com/checkout
All requests to domain.com are redirected to www.domain.com
So here is my attempt:
<IfModule mod_rewrite.c>
# Use PHP 5.4
AddType application/x-httpd-php54 .php
Options -MultiViews
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} ^(/checkout|/order)
RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L]
</IfModule>
But when I make a post to domain.com/cart the user is automatically redirected to domain.com/index.php
Please let me know where I am going wrong...
First off, you need to understand that such rules in .htaccess files are run on a first-come, first-serve basis. Also, you are using the L flag for each of them.
So, you should make sure that the two conditions and rule for silent-mapping to index.php should come last. Move those down to below the https and www-on rules.
Now, of course a request to /cart will be mapped to index.php; that's what it's programmed to do. However, you say "redirected"... Does that mean that it shows index.php in the address bar? If that is the case, the fix I've mentioned should sort that out (always check for redirects first, and then do the necessary mapping).
Like #scotsninja said in the comments, these things should be handled by Laravel itself. Your .htaccess file should only be used to map anything that is not a file or directory to the index file, or Laravel bootstrap.

.htaccess in subdirectory 'overriding' parent htaccess

been searching for 2 days and can't quite get the right solution due to my lack of understanding of mod_rewrite and time constraints on this project so hoping someone can help.
The aim
To rewrite all requests to the root index.php if the client doesn't have the correct cookie.
If the client has the correct cookie allow them to browse as they wish.
The problem
The htaccess in my subdirectory is taking precendence over my root htaccess, so requests such as www.mydomain.com/subdir/index.php arn't getting redirected.
My root .htaccess
Options FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} !^.*pass.*$
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteRule ^(.*)$ http://www.mydomain.com/index.php?url=$0 [NC]
My subdir htaccess
RewriteEngine On
RewriteBase /
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
Additional info
Ideally I'm trying to create a password protected area, so all requests are routed to index.php where a password can be entered and when verified a cookie is created, allowing free browsing of contents and sub directories. So if there is a better way to accomplish this then please let me know, and I havn't gone for .htpasswd since I need custom login, error and splash pages.
Also, the subdir .htaccess is an ExpressionEngine URL handler.
Thanks.
To allow execution of rewrite rules from parent .htaccess (htaccess from parent folder), you need to explicitly allow it (Apache will treat rewrite rules in current .htaccess as the only one that need to be executed, as long as rewritten URL remains in the same subfolder).
You need to add this line to your .htaccess in sub-folder:
RewriteOptions inherit
Apache manual: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriteoptions

url rewriting index.php

i have urls like
http://mysite.com/index.php?p=resources
http://mysite.com/index.php?p=resources&s=view&id=938
but i want urls like
http://mysite.com/resources
http://mysite.com/resources/view/938
instead of making hundreds of rewrite rules i wonder if it would be possible to just have one? Ive head this is possible by "getting the uri and splitting it into parts" and then just add a rewrite rule for index.php
but how? could someone give an example or link a tutorial
I happen to use this in .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
This essentially calls index.php no matter what is requested. Then inside your PHP code you can look at $_SERVER['REQUEST_URI'] to get the URL and parse it accordingly.
The RewriteCond lines are there to exclude direct calls to files. In my case I don't want stuff like requests for js/css/image files to go through index.php for performance reasons.
Create a .htaccess file. Not somefilename.htaccess, it is simply named .htaccess.
Note: Your index.php and .htaccess file should be in the same directory.
Now try this on your .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([A-Za-z0-9\-]+)$ index.php?p=resources
RewriteRule ^([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+)/([0-9]+)$ index.php?p=resources&s=view&id=938
</IfModule>
see more about url rewrite here

Categories