Multiple conditions in htaccess - apache

I have a htaccess file which grabs pretty much anything after the / sign and gives it to my index.php?i. This works perfectly but I want to take it one step closer and have the same htaccess use conditions.
Currently this is the navigation method mysite.com/stuff/cat
the htaccess simply grabs the string stuff/cat and my PHP can then load up contents about it. But what I want to do is: when the navigation is mysite.com/stuff/cat/images I want the htaccess to redirect to another file rather than my index.php. Is it possible to add some kind of If statement or condition in htaccess? (if string contains 'images' redirect to gallery.php?i=data)
here is my current code:
RewriteEngine On
RewriteOptions inherit
RewriteRule ^([a-zA-Z0-9/_\s'-()]+)$ index.php?i=$1

You can try the following (place it under your RewriteOptions:
RewriteRule ^([a-zA-Z0-9/_\s'-()]+)/images$ gallery.php?i=$1 [L]
I would recommend using the L flag (as above) in your other rule too.

Related

How to permanently append X to complete a shorthand URL via htaccess rewrite for a bi lingual site?

Using htaccess how can one achieve the following on a bilingual (the only two languages being: en|nl)?
if typed website.org/en (without anything after /en) then permanently redirect to website.org/en/home
if typed website.org/nl (without anything after /nl) then permanently redirect to website.org/nl/home
However, I dont want to just randomly add /home after any /* So this is only to catch incomplete shorthand urls /en and /nl, since normally all other urls always have /en/some page name.
You can use this rule as your topmost rule:
RewriteEngine On
RewriteRule ^(en|nl)/?$ /$1/home [L,NC,R=301]

How to include a single $ (dollar-sign) within the rewrite rule

I'm trying to create a rewrite rule for my .htaccess file. I want to include a single dollar sign within it. It is supposed to be something like this:
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^\$$ "http\:\/\/domain\.com\/something" [R=301,L]
The problem is that it doesn't work with a single dollar sign, so if I go to domain.com/$ I get a 404. It works only with another letter, for example ^\$a$ - in such case domain.com/$a would redirect to domain.com/something.
The workaround is to create a new folder, rename it to $ and put the .htaccess file there, but I would rather avoid creating multiple folders with no content for such purposes. I couldn't find any reference on the Internet (the official Apache documentation for mod_rewrite was not very helpful). I tried using multiple slashes in different combinations but everything failed. Am I missing something or is it just impossible to make it work this way?
For me it works exactly as expected in htaccess with RewriteRule ^\$$ and requesting "$". Have you looked at the rewritelog / loglevel rewrite:trace8 yet?

htaccess rewrite directory/file references to new url

I'm trying to rewrite all files located below a URL such as:
http://www.example.com/one/
to a new URL:
http://www.newhome.com/new/
So, the desired functionality is to have http://www.example.com/asdf and http://www.example.com/ both remain on www.example.com, but http://www.example.com/one/test/index.php would pull content from:
http://www.newhome.com/new/test/index.php
I can't quite get the correct rewrite rule to do this. Any ideas?
EDIT: The rewrite rule needs to have requests from http://www.example.com/one/* retain the URL in the browser, but pull content from http://www.newhome.com/new/test/* (the rewritten locations will include forms and form submissions).
Of note, the .htaccess file will go in the equivalent of the '/one' directory (because of server access restrictions)
Thank you
Put this rule as your first rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^one/.*$ /new/test/index.php [L,NC]

multiple folder redirect

I have been trying variations of the following without success:
Redirect permanent /([0-9]+)/([0-9]+)/(.?).html http://example.com/($3)
It seems to have no effect. I have also tried rewrite with similar lack of results.
I want all links similar to: http://example.com/2002/10/some-long-title.html
to redirect the browser and spiders to: http://example.com/some-long-title
When I save this to my server, and visit a link with the nested folders, it just returns a 404 with the original URL unchanged in the address bar. What I want is the new location in the address bar (and the content of course).
I guess this is more or less what you are looking for:
RewriteEngine On
ReriteRule ^/([0-9]+)/([0-9]+)/(.?)\.html$ http://example.com/$3 [L,R=301]
This can be used inside the central apache configuration. If you have to use .htaccess files because you don't have access to the apache configuration then the syntax is slightly different.
Using mod_alias, you want the RedirectMatch, not the regular Redirect directive:
RedirectMatch permanent ^/([0-9]+)/([0-9]+)/(.+)\.html$ http://example.com/$3
Your last grouping needs to be (.+) which means anything that's 1 character or more, what you had before, (.?) matches anything that is either 0 or 1 character. Also, the last backreference doesn't need the parentheses.
Using mod_rewrite, it looks similar:
RewriteEngine On
RewriteRule ^/([0-9]+)/([0-9]+)/(.+)\.html$ http://example.com/$3 [L,R=301]

Proper mod_rewrite for SEO friendly URLs

I have a custom php script, each having a page for page.php?id=[number] and page.php. I've read a few .htaccess tutorials but I don't know which one is the most appropriate way of doing it:
Example,
page.php?id=12 to page/id-12/and
page.php to page/ AND page
Here's what I currently have:
RewriteEngine On
RewriteRule ^category/([0-9])$ /category.php?id=$1
RewriteRule ^category/$ /category.php
Code above returns 404 not found if i access category but appears okay if i access category/ (with a slash)
All your rules include the slash / at the end of category, thus of course category cannot match it.
RewriteRule ^category/?$ /category.php
The ? after / makes it optional