Mod ReWrite to remove component of URL - apache

I must be an idiot because I just can't work this bit out.
I've got a URL:
www.site.com.au/products/product-name.html
I need to redirect these to:
www.site.com.au/product-name.html
All the links are dynamic, the folder doesn't exist. What ReWrite rule do I use to accomplish this?
This is what I've got so far:
RewriteCond %{HTTP_HOST} ^(www|test)\.site\.com\.au
RewriteCond %{REQUEST_URI} ^(/products/)
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^.+\.html$ ${lc:%{REQUEST_URI}} [NC,R=301,L]
Just need to add the bit to remote /products
Thanks.

RewriteRule ^products(/.*)$ http://www.site.com.au$1 [L, R=301]
This replaces everything you listed, except for the first RewriteCond (to match the domain, though if your VirtualHost only answers on those two domains, you can exclude that RewriteCond to simplify it).
RewriteRules are matched first before Apache looks at the RewriteConds, so if you can do the match in the RewriteRule itself it greatly simplifies things. Just for your future reference, if you did need to match in the RewriteCond, it would look something like this:
RewriteCond %{REQUEST_URI} ^/products(/.*)$
RewriteRule ^.*$ http://www.site.com.au%1 [L, R=301]
Note the %1 for matching what's in the parentheses in the RewriteCond vs. the $1 for matching what's in the RewriteRule.
EDIT: Per your comment, the following modification should force lowercase. I haven't had to do that myself, but per this Apache documentation it's an internal function via RewriteMap. Based on your original code it looks like maybe you already have the RewriteMap definition elsewhere. If not, I've included it here.
RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} ^/products(/.*)$ [NC]
RewriteRule ^.*$ http://www.site.com.au${lc:%1} [L, R=301]

OK, I really don't know enough about Apache Rewrite to figure out the exact formatting. But after much ado these are the results that worked:
# Lowercase all /products/
RewriteCond %{HTTP_HOST} ^(www)\.site\.com\.au
RewriteCond %{REQUEST_URI} ^/products/.+\.html
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^ ${lc:%{REQUEST_URI}} [R=301,L]
# Lowercase all /products/ and strip products/ subfolder
RewriteCond %{HTTP_HOST} ^(www)\.site2\.com\.au
RewriteCond %{REQUEST_URI} ^/products/.+\.html
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^products/(.+\.html)$ /${lc:$1} [R=301,L]
Thanks,
Dom

Related

htaccess, rewrite for specific subdirectory but not others below it

I have a subdirectory /categories/stories/ that I want to rewrite to /books/ but I don't want to rewrite something like /categories/stories/horror. I'm doing the following but it has no effect:
RewriteCond ${REQUEST_URI} ^/categories/stories[/]?
RewriteCond ${REQUEST_URI} !^/categories/stories/+[/]?
RewriteRule ^(.*) /books/ [R=301,L]
The result being if a person goes to, for example, http://bookstore.com/categories/stories they go to http://bookstore.com/books but http://bookstore.com/categories/stories/horror there's not rewrite.
You can just use anchors in your regex pattern:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/categories/stories/?$ [NC]
RewriteRule ^ /books/ [R=301,L]
Pattern ^/categories/stories/?$ will match /categories/stories/ or /categories/stories but not /categories/stories/anything.
Also make sure to clear your browser cache or use a different browser for testing this change.

three conditions and one rule to use any domain name in htaccess file

I have these conditions and rule in my htaccess file, and it works and does what it's supposed to do.
RewriteCond %{REQUEST_URI} !^/index/parser.php?(.*)?$
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.extension$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-.]+)\.mydomain\.extension [NC]
RewriteRule ^(.*)$ http://mydomain.extension/index/parser.php?%2&domain_name=mydomain.extension [QSA,L]
I want to rewrite it so it can work with any domain name pointed at the server, but don't know how. I tried this but it failed. It should only activate on subdomains, not root domains. Also, I am not versed on the whole dollar sign and percentage usage in htaccess files. $1, $2, %1, %2, etc. Never know when dollar or percent should be used, or what number to use after it.
RewriteCond %{REQUEST_URI} !^/index/parser.php?(.*)?$
RewriteCond %{HTTP_HOST} ^(.*)\.(.*)$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-.]+)\.(.*) [NC]
RewriteRule ^(.*)$ /index/parser.php?%2&domain_name=%1 [QSA,L]
Edit
Tried this on last line. Still didn't work.
RewriteRule ^(.*)$ http://%1/index/parser.php?%2&domain_name=%1 [QSA,L]
Edit
Tried suggestion and it made this:
something.tld
Go to this:
http://tld/index/parser.php?something&domain_name=tld
Should be:
http://something.tld/index/parser.php?something&domain_name=something.tld
Edit
The next version didn't work either. Showed an internal server error page. Same error whether domain.tld was used or subdomain.domain.tld was used.
How about:
RewriteCond %{REQUEST_URI} !^/index/parser.php
RewriteCond %{HTTP_HOST} ^(?:www\.|)([a-z0-9-]+)\.(.+\.(?:[a-z]{2}\.[a-z]{2}|[a-z]+))$ [NC]
RewriteRule ^(.*)$ http://%2/index/parser.php?%1&domain_name=%2 [QSA,L]
So this will take a request for:
http://www.foo.some-domain.tld/anything
and redirect to:
http://some-domain.tld/index/parser.php?foo&domain_name=some-domain.tld
As far as the backreferences go, the $ ones backreference groupings made in the rewrite rule, while the % ones backrefernce groupings from a rewrite condition.
See: http://httpd.apache.org/docs/trunk/rewrite/intro.html#regex

Simple Apache mod_rewrite remapping

I have to remap a few ID's to URL strings (301 redirect) and I have to do it with mod_rewrite:
/page.php?id=15 to /pagexy
/page.php?id=10 to /pageyz
The rule:
RewriteRule ^page.php?id=15$ /pagexy [L,R=301]
doesn't work. What am I doing wrong?
You need to inspect the query string separately. The following should work:
RewriteEngine On
RewriteCond %{QUERY_STRING} id=15\b [NC]
RewriteRule ^/page.php$ /pagexy? [L,R=301]
RewriteCond %{QUERY_STRING} id=10\b [NC]
RewriteRule ^/page.php$ /pageyz? [L,R=301]
Given you are tying ids to pages, it may also pay to look at using a RewriteMap

Something wrong in my .htaccess/mod_rewrite rule?

Next simple rule at .htaccess for redirect all requests with non-www to www:
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
and when check it with the URL of kind
http://example.com/something
this redirect me to
http://www./something
seems that RewriteCond works, but there is nothing in result of regular expression, where as i think must be the host name without www. What's wrong - who knows?
Variants of rules with redirect to concrete host, like:
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
and other alike variants works fine.
Instead of capturing the HTTP_HOST into %1 with (.*) (which isn't working for you), just substitute %{HTTP_HOST} in the RewriteRule:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
The way you had it, your %1 is not populated because of the ! negation, it would seem.
From the RewriteRule documentation:
When using the NOT character to negate a pattern, you cannot include grouped wildcard parts in that pattern. This is because, when the pattern does NOT match (ie, the negation matches), there are no contents for the groups. Thus, if negated patterns are used, you cannot use $N in the substitution string!
This standard way of doing this seems to be:
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*) http://www.example.com/$1 [R=301]
This is how mediatemple, wordpress, etc. recommend their users setup their .htaccess files.

Apache rewrite based on subdomain

I'm trying to redirect requests for a wildcard domain to a sub-directory.
ie. something.blah.example.com --> blah.example.com/something
I don't know how to get the subdomain name to use in the rewrite rule.
Final Solution:
RewriteCond %{HTTP_HOST} !^blah\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)
RewriteRule ^(.*) /%1/$1 [L]
Or as pointed out by pilif
RewriteCond %{HTTP_HOST} ^([^.]+)\.blah\.example\.com$
You should have a look at the URL Rewriting Guide from the apache documentation.
The following is untested, but it should to the trick:
RewriteCond %{HTTP_HOST} ^([^.]+)\.blah\.domain\.com$
RewriteRule ^/(.*)$ http://blah.domain.com/%1/$1 [L,R]
This only works if the subdomain contains no dots. Otherwise, you'd have to alter the Regexp in RewriteCond to match any character which should still work due to the anchoring, but this certainly feels safer.
Try this:
RewriteCond %{HTTP_HOST} (.+)\.blah\.domain\.com
RewriteRule ^(.+)$ /%1/$1 [L]
#pilif (see comment): Okay, that's true. I just copied a .htaccess that I use on one of my projects. Guess it has a slightly different approach :)
#Sam
your RewriteCond line is wrong. The expansion of the variable is triggered with %, not $.
RewriteCond %{HTTP_HOST} ^([^\.]+)\.media\.xnet\.tk$
^
that should do the trick