How to rewrite a space (%20) in .htaccess - regex

I am trying to get some old URL's to redirect to the homepage. I tried it like below but it doesn't appear to work. Also tried some regex, but no results.
RewriteRule ^blog/%20article%20name(/?)$ / [R,L]
How is this to be done?

Converting my comment to answer so that solution is easy to find for future visitors.
To be able to match whitespace in URI, you can use perl property for whitespaces \s in your rule like this:
RewriteRule ^blog/\sarticle\sname/?$ / [R=302,NC,L]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

Related

What does (|/)$ do with mod_rewrite?

I've seen examples in htaccess files using mod_rewrite where everything is done through one php file and different URLs are redirected back to index php.
RewriteRule ^registration(|/)$ /index.php
I'm curious as to what (|/)$ does/is. I've read a lot of stuff and can't seem to find any mention of the use of a vertical bar in mod_rewrite and if I remove this, the redirect still works fine.
The vertical bar stands for a logical OR, and lets you specify either a trailing slash after 'registration' or not.
I prefer using a '?' after the slash, making it optional:
RewriteRule ^registration/?$ /index.php

Apache RewriteRule for removing tailing slash only at root

I know there are many questions about Apache RewriteRules, especially for removing trailing slashes. I have looked at tons but I can't seem to find anyone trying to solve this problem.
I am using Magento, so the URL structure looks like this:
example.com/index.php
example.com/index.php/
example.com/index.php/page1/
Here is my ideal URL structure:
example.com
example.com/page1/
example.com/page2/
So basically I just want to strip the index.php AND make sure the naked domain does not have a trailing slash (example.com instead of example.com/). Also, I would like to NOT include the hardcoded domain name if possible so that the rewrite can be applied in different environments.
Here is my current Rewrite...
RewriteCond %{REQUEST_URI} ^/index\.php/?
RewriteRule ^index.php/(.*) /$1 [R=301,L]
This seems to work in all situations, except for:
example.com/index.php (doesn't work at all)
example.com/index.php/ (leaves the trailing slash)
I would appreciate any regex advice! Thank you.
UPDATE
Thanks to the answer below from #zx81 I have successfully stripped all URLs down to the root domain, but still can't remove the slash.
So here is the current URL: example.com/
And I can't remove the trailing slash!
Not able to test it live, but try this.
RewriteRule ^index\.php()/?(?:([^/]+)/)? $1$2 [R=301,L]
It should handle the one that doesn't work at all thanks to the empty capturing group 1 ().
In PCRE (Apache's regex flavor) this also strips the trailing slash, but Apache may decide to add it back.

Htaccess Not Working - Redirect 301

Due to some bad URLs, we generated some links that don't work and I want to redirect them with a 301 redirect to clear up some webmaster tools issues with Google.
So, we have this URL like this:
http://www.site.com/subdomain/z//-products
*Note that subdomain is variable, the rest of the url is static.
As a side note, this URL makes no sense, that's why I want to redirect it. It should be something like this:
http://www.site.com/bedroom/z/12345/bedroom-furniture-products
Anyway, we had these bad URLs being dynamically generated. We've fixed them, but google picked them up and keeps trying to crawl them. I want to create an htaccess rule to 301 redirect them and the issue should wash out eventually.
Here's what I tried with htaccess to no avail:
^(.*)/n//-products/?$ $1 [R=301,B]
I've also tried all kinds of permutations of this and it's not working. I suspected it was an entity escaping issue, but my research led me to add the [B], but that didn't seem to work either. It's like the redirect rule is working, but it's just redirecting to the original page.
What am i missing here?
I believe anubhava is correct, in that there is inconsistency between the sample URL you describe /subdomain/z//-products and the RewriteRule you attempted to apply. Not sure if this is a typo or not. It may even be the case your copy/paste operation actually added the "/n" literally.
Anyhoo, let us presume that you want to make the rule work with /subdomain/z//-products:
RewriteRule ^/([^/]+)/z//\-products/?$ http://www.site.com/$1 [R=301]
See the example 1 slides of this PDF to get the quick first portion. It is much faster than using (.*).
We literally match the z character and the surrounding slashes. We escape the - character, then we do the rest of the URL and optionally match the trailing slash. We use, if memory serves correctly, an "external" style redirect so that the robots re-open a separate HTTP connection, appending the matched backreference, and hand off the status code.
Let me know if that works.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^subdomain/z/-products/?$ /$1 [L,R=302,NC]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

mod_rewrite performing unexpectedly

I can't figure this one out no matter how many times I google it or think about it. I have a RewriteRule in my .htaccess file: RewriteRule ^/download/([^/\.])/?$ /downloadfile.php?f=$1 [L]
When I use this, my page loads fine, but going to the link http://www.example.com/download/file.ext, it pulls a 404 page. However, if I load the page, then change my RewriteRule to RewriteRule ^/download/([^/\])/?$ /downloadfile.php?f=$1 [L] (noting the RegEx change), the link works exactly how I expect it to... Until I reload the page, which results in a 500 error because of a bad regex expression? (I checked my Apache error log, thats how I know it reads as a bad regex)
So, what can I do to make this work? I've tried (.*) and ([.*]) for regex as well, that didn't work either.. can anyone tell me what I'm doing wrong?
mod_rewrite strips out the prefix (the leading slash) from the URI when you use it in an .htaccess file. Your regex needs to have it removed:
RewriteRule ^download/([^/\.]*)/?$ /downloadfile.php?f=$1 [L]
This should work:
RewriteRule ^/?download/([^/]*) /downloadfile.php?f=$1 [L]
The leading slash is required in apache 1.x and stripped in version 2.x
You were also denying the dot character.

Need more info with helicon isapi and regex rule

I am working on a helicon rule and tried various combinations but they didn't work
I want the following URL to be resolved.
It can be this
www.test.com/myownpages/
or
www.test.com/myownpages
www.test.com/myownpages/?value1=test2&value2=test2
it should be resolved to
$1/test.aspx [NC]
If anyone gives something after myownpages, it shouldn't work
www.test.com/myownpages/test (This shouldn't work)
It tried the below so far
RewriteRule ^(.*)(\/\myownpages\/)(.*)(\?)?(.+)?$ $1/test.aspx [NC]
I am not very familiar with these rewrite rules, but maybe I can help with the regex. As I read it, you want to match any string ending with "/myownpages", "/myownpages/", or "/myownpages/?anything" and capture the part before that.
I'd use
^(.*)/myownpages(/([?].+)?)?$
to get this. See it in action at RegExr. If you need to escape the forward slashes, it becomes.
^(.*)\/myownpages(\/([?].+)?)?$
Note that this will not preserve the values in the query string; it will rewrite www.test.com/myownpages/?value1=test2&value2=test2 to www.test.com/test.aspx.
In case you want rewrite (NOT redirect) from /myownpages --> /myownpages/test.aspx, try using:
RewriteEngine on
RewriteBase /
RewriteRule myownpages/?$ /myownpages/test.aspx [NC,QSA,L]
QSA-flag appends the query string to the source path automatically.