htaccess Rewrite rule not working as I expected - .htaccess

I have the following rule:
RewriteRule ^(.*)$ index.php?ip=$1 [L]
But it's not working, instead it is loading:
https://example.com/?ip=index.php
Am I missing something?

Your rule is looping because there is no condition to stop rewriting for existing files and directories.
After first rewrite it becomes:
index.php?id=1.2.3.4
and after second rewrite URI becomes:
index.php?id=index.php
You can use this rule to fix this behavior:
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?ip=$1 [L,QSA]

Related

503 htaccess file only partially working

Using the following as a 503 forwarder on a wordpress website. For some reason it words for foo.com/xyz, but not foo.com itself. Any thoughts?
.htaccess file:
ErrorDocument 503 /rdi/index.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /rdi/index.php [R=503,L]
Any url that ends with a / requests a folder, and any url that does not end on a / requests a file. When you request http://example.com, you actually request http://example.com/, the document root of example.com. Your document root is obviously an existing folder, since otherwise you wouldn't be able to run a site at all.
You are using the following rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /rdi/index.php [R=503,L]
The second condition says: "If the requested filename is not a directory". Your document root is a directory, so that condition is false. It won't rewrite.
You can use the following rule:
RewriteCond %{REQUEST_URI} ^/$ [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /rdi/index.php [R=503,L]
What will this do? If you request http://example.com, the first condition will match, the second will be ignored because of the [OR] flag, and the third condition will be true. If you request http://example.com/rdi/, the first condition will be false and the second condition will be false and so the rule will not be used. If you request http://example.com/asdfasdf/, the first condition will be false, but due to the [OR] flag it will try the second condition, which is true. Then it checks the third condition which is true, which will then rewrite the request.

Basic ReWrite Rule Request that is not a redirect

The user types the following into the browser and this remains the url in the browser:
xyz.com/info/x/
Internally this becomes:
xyz.com/x/
What I have is:
RewriteRule info/x/ http://xyz.com/x/ [L,NC,R=302]
Problem with the above:
This is a redirect but I desire the browser to still show xyz.com/info/x/
No need for variables, I just want to handle one case.
Get rid of the hostname and the R flag:
RewriteRule ^info/x/ /x/ [L,NC]
Or to be more general
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^info/(.*)$ /$1 [L,NC]

Multiple rewrite rules in htaccess

I have an .htaccess file that rewrites urls for SEO purposes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /display.php?page=$1 [L,NC,QSA]
RewriteRule ^search/([^/\.]+)/?$ search.php?what=$1&where=$2 [L,NC,QSA]
Now the first rewrite rule works fine. (www.domain.com/user goes to display.php?page=user)
but the second one should work like (www.domain.com/search/something/else must go to search.php?what=something&where=else
What am I doing wrong here?
Your second rule is incorrect for what you are looking for. You are requesting the result of two captures, but are only making one capture.
Try this instead:
RewriteRule ^search/([^/\.]+)/([^/\.]+)/?$ search.php?what=$1&where=$2 [L,NC,QSA]
Edit: You'll also need to switch your rules around. Your first rule captures everything, and would therefore discard the second.
So, swap them around, and use the L flag, as suggested already.
You are using the [L] flag, which causes mod_rewrite to stop processing the rule set.
Try removing the L flag from the first Rewrite Rule, like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /display.php?page=$1 [NC,QSA]
RewriteRule ^search/([^/\.]+)/?$ search.php?what=$1&where=$2 [L,NC,QSA]

Rewrite Rule Loop. How to stop applying rules

I have an .htaccess file redirecting blog post urls like /blog/2012/11/30/this-post to /blog/post.php?id=this-post. Works fine until I apply a second rule below it that is also a match. This rule is set to take a path formatted url like /this/is/a/pageid and redirect to /page.php?id=pageid. It doesn't care how long the path is, it just uses the last directory in the path as the id. Unfortunately, this rule matches everything and I'm not sure how to stop the redirect after the first match. Here is my .htacess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/([0-9]+)/([0-9]+)/([0-9]+)/([A-Za-z0-9-]+)$ /blog/post.php?id=$4 [L]
RewriteRule ([^/]+)/?$ /page.php?id=$1 [L]
Thanks in advance for the help.
You can add an optional query parameter after you redirect the first time and combine that with a simple rewrite condition on the query string to check if that parameter exists or no, try this :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/([0-9]+)/([0-9]+)/([0-9]+)/([A-Za-z0-9-]+)$ /blog/post.php?id=$4 [L]
RewriteCond %{QUERY_STRING} !^(.*&)?r=0(&.*)?$
RewriteRule ([^/]+)/?$ /page.php?id=$1&r=0 [L]

htaccess rule is passing innecesary variable

This is the file tree:
/problem/
/problem/index.php
index.php
category.php
somefile.php
I have this 2 rules in the .htaccess that is sitting in the /
RewriteRule ^somedir$ /somefile.php [L]
RewriteRule ^([a-z-]+)$ /category.php?cat=$1 [QSA,L]
So...
http://domain.com/somedir = OK
http://domain.com/ = OK
http://domain.com/problem/ < automatically adds ?cat=problem to the querystring. I want to avoid that extra ?cat=problem
I need to add a rule that doesn't add the cat=$1 when the /dir/ exists.
Just add a RewriteCond before your second rule. Basically, don't run that catch all if it starts with product:
RewriteRule ^somedir$ /somefile.php [L]
RewriteCond %{REQUEST_URI} !^/product [NC]
RewriteRule ^([a-z-]+)$ /category.php?cat=$1 [QSA,L]
To prevent redirecting for a real file or directory, add these two lines before the rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

Resources