htaccess rewrite rule with strings that contain slashes - apache

I have a mod rewrite rule like the following.
RewriteRule ^bands/([^/]*)$ /folder/index.php?palabra=$1 [L]
It works except when someone access a link that uses a word with a / symbol on the band name, for example the band ac/dc would not work and shows a 404 error.
Im guessing some kind of problem with the regex.
How can I avoid this problem?

Related

Redirecting Many Dynamic URLs (301)

I have a website that is generating dynamic URLs through categories and it outputs the same information on two separate URLs (In this example it's "buildings" and "houses")
I would like to redirect all URLs that have /buildings/ in the URL to the same one with /houses/ instead.
For example:
/buildings/united-states/arizona/tucson/
to
/houses/united-states/arizona/tucson/
There are many URLs like this and I would like to use a code that does this for all.
I have tried
RewriteRule ^buildings/(\d[^/]+) /houses/$1/ [R=301,L], but it didn't seem to work (it still pointed to the /buildings/ URL.
Appreciate all your comments and guidance, thank you!
RewriteRule ^buildings/(\d[^/]+) /houses/$1/ [R=301,L]
For some reason have a \d (shorthand character class) that matches digits 0-9 only, so this won't match the example URL. Also, [^/]+ would seem unnecessary if it can be followed by anything anyway.
Try the following instead:
RewriteRule ^buildings/(.*) /houses/$1 [R=302,L]
This matches /buildings/<anything>. The $1 backreference holds the <anything> part.
Test first with 302 (temporary) redirect to avoid potential caching issues and only change to a 301 (permanent) redirect once you have confirmed it works as intended. You should clear you browser cache before testing.
This needs to go near the top of your .htaccess file, before any existing rewrites.

.htaccess -> Characters not escaping in rewrite string "(" ")"

I'm currently trying to add some redirects from a very old site that had PDFs and some of these PDFs have used brackets to contain the years.
For example:
/document%2070C11-name%20(2012).pdf
So the full URL I am trying to redirect is:
https://website.co.uk/document/1/document%2070C11-name%20(2012).pdf
I have previously setup many different redirects on this site but ones with the special characters seem to be causing an issue.
Here is the examples of what I have tried, and failed with so far:
RewriteEngine on
RewriteBase /
RewriteRule ^document/1/document%2070C11-name%20\(2008\).pdf$ document/newurl/ [R=301,L]
RewriteRule ^document/1/document%2070C11-name%20(.*)2008(.*).pdf$ document/newurl/ [R=301,L]
So I tried escaping the character and just trying a wildcard. Neither seemed to work on my Apache server. The code I used did seem to work when I tested it on:
http://htaccess.madewithlove.be/
But I am pretty stuck now and would love any help I can get.
Thanks,
Kane
To match %MN character in URL, you need to use \xMN in the RewriteRule pattern.
Hence this rule should work for you:
RewriteRule ^document/1/document\x2070C11-name\x20\(2008\)\.pdf$ /document/newurl/ [R=301,NC,NE,L]
This will redirect https://website.co.uk/document/1/document%2070C11-name%20(2008).pdf to https://website.co.uk/document/newurl/

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?

how to remove trailing %20target= from url using htaccess

Recently i have been using stackoverflow to fix many of my server errors. Recently i have encountered the following error "NOT FOUND 404 error" for the following error.
I have seen from web master central that there are several hundred urls have trailing string "%20target=" I want to remove so that the url yield results.
here is the sample. i wanted to change the following url from
www.example.com/one/two/three/four/five/For-Sale-26264.html%20target=
to
www.example.com/one/two/three/four/five/For-Sale-26264.html
How can i achieve this using htaccess
The %20 is actually a URL encoded space, so you can use a lazy select (.+?) to match as few characters as possible before the space with \s and then target. The grouped match is available as a back reference with $1 for the rewrite.
RewriteEngine On
RewriteBase /
RewriteRule (.+?)\starget= $1 [L]

How do you match percent symbols in rewrite rules?

I am redirecting URLs for a website, and have the following URL to match and redirect:
example.com/test-string-%2526-more-string/
However, I cannot match the %2526 portion of the string - it seems that Apache is treating the percentage symbol as a special character, and escaping it seems to make no difference. Here's the basic rule I'm trying:
RewriteRule ^test-string-\%2526-more-string/?$ /new-location.htm [R=301,L,NC]
With or without the escaping slash, this rule doesn't fire. I suspect the problem is with how apache processes encoded characters in URLs - %25 translates to a percentage symbol itself, so could Apache be translating "%25" to "%" before running the rule? Or is something else going on?
I experimented with lots of rewrite rules and in the end just went with this:
RewriteRule ^test-string-\%26-more-string/?$ /new-location.htm [R=301,L,NC]
So the %25 was converted to a % symbol.