Multiple rules of htaccess - .htaccess

I'm trying to use an htaccess file with 2 rules. The first one is working, but the second displays a white page with the URL in browser correct. If I delete first, rule the second one works. I don't know how to make it work with more than one rule.
RewriteEngine On
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^([a-z0-9]+)$ /profile.php?username=$1
RewriteRule ^([a-z0-9]+)$ /display.php?fly_id=$1

Related

.htaccess rewrite with multiple parameters

I had a rule in my .htaccess that makes URLs for articles more friendly looking for the purposes of SEO and the like.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(news-and-views)/(.+).php /$1/article.php?title=$2 [L]
Which converted this URL:
/news-and-views/going-for-brokering.php
To this within the application itself:
/news-and-views/article.php?title=going-for-brokering
Now I need a URL with an ID before the title like this:
/news-and-views/123456789/going-for-brokering.php
So I tried the following rule:
RewriteRule ^(news-and-views)/(.+)/(.+).php /$1/article.php?Id=$2&title=$3 [L]
However, this isn't working, am I misunderstanding the use of the brackets as I thought everything between them was acknowledged as a variable on the right-hand side?
I'm thinking it could even be that the less specific rule is above the more specific rule.
You need to be careful about the order of the rules, since your first rule will also match /news-and-views/123456789/going-for-brokering.php. Change your rules as follows:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(news-and-views)/([^/]+).php /$1/article.php?title=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(news-and-views)/([0-9]+)/([^/]+).php /$1/article.php?Id=$2&title=$3 [L]

Multisites with one host using htaccess. Need to create an images folder rewrite

I have one hosting account that I am running using to run multiple sites, masterhost.com. Under masterhost.com I have site1.com, site2.com, etc... and am using this piece of code in the masterhost directory to reroute the domain request from the masterhost directory to the site1 directory.
RewriteCond %{HTTP_HOST} ^(www.)?site1.com$
RewriteCond %{REQUEST_URI} !^/site1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site1/$1/
RewriteCond %{HTTP_HOST} ^(www.)?site1.com$
RewriteRule ^(/)?$ site1/index.php
However Joomla on site1.com is having some issues with with this, and is routing the default images path back to masterhost.com/images.
Is there a rewrite I could use that would recognize any request http://www.site1.com/images and write it to http://www.masterhost.com/site1/images?
I believe the problem is these two lines:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Basically, if the image exists on mainsite.com, these rules will prevent the request from being redirected to site1. So, for example, if you have mainsite.com/img/logo.png and site1.com/img/logo.png, site1.com would show the logo for mainsite.com.
Also, with this rule:
RewriteRule ^(.*)$ /site1/$1/
I don't believe the trailing slash should be there.
Finally, you didn't ask about it, but the last rule probably also doesn't work? If it does, removing the !-f and !-d checks above will break it. This would be because if a user requests site1.com/, the first rule redirects that to /site1/, which will never match the rule ^(/)?$. So, that rule needs to be changed to:
RewriteRule ^site1(/)?$ site1/index.php
You may or may not need a slash between ^ and site1 in that rule, depending on your RewriteBase directive.

htaccess does not work rewrite rule

I've been struggling with my .htaccess file for weeks now, I changed it many times but it just won't work.
I have this in my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^/([^./]+)\.html$ category.php?id=$1
RewriteRule ^/([^./]+)\.html$ tag.php?id=$1
RewriteRule ^/([^./]+)\.html$ play.php?id=$1
but it does not work.
Are you sure that mod_rewrite is turned on in Apache? Do you have access to httpd.conf? It would be better to do redirects there instead of with a .htaccess file.
Your conditions are only being applied to the first rule. Each set of RewriteCond's only get applied to the immediately following RewriteRule. So the conditions only get applied to RewriteRule ^/([^./]+)\.html$ category.php?id=$1 and the last 2 rules have no conditions at all.
Your conditions is to rewrite something that exists to something else, which will cause a rewrite loop. You probably wanted:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Your 2nd and 3rd rules will never be applied because if someone requests /some-page.html the first rule's regex will match it and rewrite the URI to /category.php?id=some-page, then the next to rules will never match because the first rule already rewrote the URI to category.php.
Your regular expressions match a leading slash, URI's being applied in rewrite rules that are inside an htaccess file has the leading slash stripped out, so you want this instead:
RewriteRule ^([^./]+)\.html$ category.php?id=$1
1, 2 and 4 is easy. 3, not so much. You're going to have to figure out a unique way to represent an html page as a category, tag, or play. You can't have all 3 look identical, there's no way to tell which one you want. Take:
/something.html
Is that supposed to be a category? A tag? or a Play? Who knows, your rewrite rules surely don't. But if you preface each with a keyword, then you can differentiate:
/category/something.html
/tag/something.html
/play/something.html
And your rules would look like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([^./]+)\.html$ category.php?id=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^tag/([^./]+)\.html$ tag.php?id=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^play/([^./]+)\.html$ play.php?id=$1

mod rewrite - allow full stops

I need a rewrite rule that allows full stops. How can I allow for this? What I want is for
shop/domain/www.test.com to become shop/domain/controller.php?param1=www.test.com
The original rule I have is below:
RewriteRule ^shop/domain/([^/.]+)/?$ shop/stock/controller.php?param1=$1 [NC]
This works but only if I remove the full stop.
I have also tried the following:
RewriteRule ^shop/domain/([^/]+)/?$ shop/domain/controller.php?param1=$1 [NC]
This allows the full stop but then has controller.php is the parameter when it should be www.test.com.
If all domains begin with "www.", you could do this
RewriteRule ^shop/domain/www\.([^/]+)/?$ shop/domain/controller.php?param1=$1 [NC,L]
The problem with your rule is, that it also matches controller.php as if it were a domain. You can add the following two rewrite conditions to prevent this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^shop/domain/([^/]+)/?$ shop/domain/controller.php?param1=$1 [NC,L]
!-d means not an existing directory, !-f means not an exisiting file (e.g. controller.php).

htaccess directory to file redirect problem

I’m trying to use the following .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^images/
RewriteRule (.*) view.php?picid=$1 [L]
RewriteRule ^/user/(.*)$ /users.php?user=$1
I want two things to happen: Whenever someone requests /1234, it redirects to /view.php?picid=1234, and also when someone visits /users/bob, it redirects to /users.php?user=bob.
My code however, doesn’t seem to be working correctly.
There are several ways to do that. Here’s one that should work:
RewriteRule ^user/(.+)$ users.php?user=$1 [L]
RewriteRule ^([0-9]+)$ view.php?picid=$1 [L]
The first rule will catch any request that’s URI path begins with /user/ followed by one or more arbitrary characters. And the second will catch any request that’s URI path begins with / followed by one or more digits.
The initial problem with your rules is that the RewriteRule with (.*) will match everything.
If you do not want it to match a URL with a slash in it (such as users/bob), try ^([^/]*)$
Secondly, after a URL is rewritten, the new URL goes through your rules again. If you want to avoid matching something that has already been rewritten once, you should add a condition like
RewriteCond %{REQUEST_URI} !\.php

Resources