mod-rewrite issue with spaces in the url - regex

RewriteRule ^categories/([A-Za-z0-9\-]+)/?$ /categories.php?c=$1 [QSA,L]
This is my RewriteRule, it deals with categories such as /categories/Family perfectly that URL displays the page as I would like it too, However with something such as /categories/Web%20Design I get an The requested URL /categories/Web Design was not found on this server.
This is a pain, I've even tried to use a space in the ReWriteRule after the 9 in [A-Za-z0-9-], what's the best way to handle spaces in URLs with the rewrites?
Thank You All.

Use this rule by including space in your character class:
RewriteRule ^categories/([A-Za-z0-9\s-]+)/?$ /categories.php?c=$1 [QSA,L]

try adding \s to the Regex...
That matches whitespace.
([A-Za-z0-9\-\s]+)

Related

How to write expression to grab all after an expression and then rewrite in htaccess

I'm new to the rewriting of urls and regex in general. I'm trying to rewrite a URL to make it a 'pretty url'
The original URL was
/localhost/house/category.php?cat=lounge&page=1
I want the new url to look like this:
/localhost/house/category?lounge&page=1
(like I say, I'm new so not trying to take it too far at the moment)
the closest I've managed to get it to is this:
RewriteRule ^category/(.*)$ ./category.php?cat=$1 [NC,L]
but that copies the whole URL and creates:
/localhost/house/category/house/category/lounge&page=1
I'm sure, there must be an easy way to say copy all after that expression, but I haven't managed to get there yet.
I will try to help you:
You probably have already, but try a mod rewrite generator and htaccess tester.
From this answer: The query (everything after the ?) is not part of the URL path and cannot be passed through or processed by RewriteRule directive without using [QSA].
I propose using RewriteCond and using %1 instead of $1 for query string matches as opposed to doing it all in RewriteRule.
For your solution, try:
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^house/category$ house/category.php?cat=%1 [NC,L]
This will insert the .php and cat= while retaining the &page=
Anticipating your next step, the below mod rewrite may help get started in converting
http://localhost/house/category/lounge/1
to
http://localhost/house/category.php?cat=lounge&page=1
Only RewriteRule necessary here, no query string:
RewriteRule ^house/category/([^/]*)/([0-9]*)/?$ house/category.php?cat=$1&page=$2 [NC,L]
Use regex101 for more help and detailed description on what these regexes do.
If it still not working, continue to make the regex more lenient until it matches correctly:
Try to remove the ^ in RewriteRule so it becomes
RewriteRule category$ category.php?cat=%1 [NC,L]
Then it will match that page at any directory level. Then add back in house/ and add /? wherever an optional leading/trailing slash may cause a problem, etc.
Thanks for all your suggestions, I took it back to this
RewriteRule category/([^/])/([0-9])/?$ category.php?cat=$1&page=$2 [NC,L]
which has done the trick, and I'll leave it at this for now.

Rewrite rule regexp

I would like to ask you guys because I have a problem with one my rewrite rules and I can't figure it out how to write the good one.
I have this rule:
RewriteRule ^wp-content/uploads/(.*[^(.js|.swf)])$ authenticate.php?file=$1
What I would like to do is redirect the user to the authenticate.php every time when someone tries to open something in the wp-content uploads dir and I would like to send the filename to the php
For example:
http://domain.tld/wp-content/uploads/2015/11/something.pdf
redirect to authenticate.php?file=something.pdf
But unfortunately my regexp is broken. Could someone help me?
Really thanks for it!
Try with that in your .htaccess:
RewriteCond %{REQUEST_URI} !(?:js|swf)$ [NC]
RewriteRule ^wp-content/uploads/(.+)$ authenticate.php?file=$1 [NC,L]
For http://domain.tld/wp-content/uploads/2015/11/something.pdf the result: http://domain.tld/authenticate.php?file=2015/11/something.pdf
Try using this regex with negative lookaheads:
^.*?wp-content\/uploads\/.*?\.(?!js$|swf$)[^.]+$
The following URL will match the regex:
http://cpsma.ie/wp-content/uploads/2015/09/CPSMA-Newsletter-No-35-Sept-2015-2.pdf
However, a similar URL which ends in .js or .swf will not match. Hence, the following two URLs do not match the regex:
http://domain.tld/wp-content/uploads/2015/10/javascript.js
http://domain.tld/wp-content/uploads/2015/02/shockwavefile.swf
You can test this regex out here:
Regex101

Unescapable Periods in Apache 2.0 RewriteRule Regex

So, I have the following RegEx..
RewriteRule ^([-a-z0-9]*[A-Z\.]+.*)$ file.php?string=$1 [QSA]
The URL I want file.php to trigger for must either have capital letters or a period in it, then send the URL to the PHP script.
However, the problem I have is that this script is triggering on any URL, because of the not-truly-escaped Period.
I've tried escaping the period with a backslash, or two backslashes, or three... but none stop the generic interpretation.
What am I doing wrong?
Edit: As an example,
RewriteRule ^([-a-z0-9]*[A-Z\\.]+[-a-z0-9\/]*)$ file.php?string=$1 [QSA]
Doesn't work, but
RewriteRule ^([-a-z0-9]*\\.+[-a-z0-9\/]*)$ file.php?string=$1 [QSA]
does escape it.
Edit 2: Examples of URLs I want to redirect:
/some-page-goes-here.html
/heres-Robs/Old/Page/
And ones I don't:
/testing/one/two/
/an/actual-file.gif
EDIT 3: Old regex was:
RewriteRule ^([-a-z0-9]*[A-Z\.]+[-a-z0-9\/]*)$ file.php?string=$1 [QSA]
But while writing the post, I updated the question's regex to what you see above.
Try:
RewriteCond %{REQUEST_URI} [A-Z] [OR]
RewriteCond %{REQUEST_URI} \.html$
RewriteRule (.*) file.php?string=$1 [QSA]
When using mod_rewrite and you have several URLs to match, it is always better to use RewriteCond to filter and then apply your RewriteRule.
I don't think your problem can be what you think it is: periods in a character class are supposed to mean literal periods, not "any character". If this really is the problem, somehow, then you could change [A-Z\.]+ to ([A-Z]|\.)+; but I doubt it. Some things to try:
what happens if you comment out this line? does that successfully disable this redirect? if not, then obviously the problem isn't with this line. :-)
what happens if you make this a real HTTP redirect, by changing QSA to QSA,R? Does the destination URL look like what you expect? Maybe there are some unexpected periods or uppercase letters? (Warning: this will very likely trigger an infinite redirect loop if you try it in a browser; it'll probably be easier to try submitting the request via port-80 Telnet and seeing the actual HTTP response.)
Also, your rule doesn't quite match how you describe it. For example, your rule wouldn't match a URL like a.b.c, because you only uppercase letters and/or dots to occur in a single "clump"; if they're separated by lowercase letters, no match will occur. Is that just because you didn't want to overcomplicate the description?

How to check for dot(.) in mod_rewrite

I want to redirect URL
domain/Family_He..
to
domain/Family_Health_insurance
using RewriteRule. I have tried with
RewriteRule /Family_He(.*)$ /Family_Health_insurance
and it is working. But I have some more page with urls like
domain/Family_Health_info
domain/Family_Health_quote
domain/Family_Health_child etc
When I tried as
RewriteRule /Family_He\.\.$ /Family_Health_insurance
then this won't works for me. Please help me out.
Are you aware that there are actually two spaces at the end of your subject string that would prevent \.$ (a literal dot at the end of the string) from matching?
To have it redirect (302) you need to add the R flag. To match the dot you need to escape it using \. like
RewriteRule ^/Family_He\.\.$ /Family_Health_insurance [R=302,L]
Note that the leading slash doesn't work in htaccess, only in httpd.conf

Quick mod-rewrite/regex issue

I am trying to setup my .htaccess file to do some nifty redirects for me.
Right now I have URLs like:
mysite.com/?video=1
I would like to have URLs like:
mysite.com/1/
Right now I have pieced together the following regex:
RewriteRule ^(.*)/?$ /index.php?v=$1 [L]
This works great if the URL is in the format
mysite.com/2
, but NOT if the format is
mysite.com/2/
, NOTE the trailing slash.
So what I really need help with is my regex! :)
Try to make the quantifier non greedy
^(.*?)/?$
otherwise the trailing slash is matched by the ., because its greedy and the explicit slash is optional.
Stema's idea should work. Or you could just make the regex more specific by e.g. only accepting numbers.
RewriteRule ^([0-9]+)/?$ /index.php?v=$1 [L]
or alpha numeric
RewriteRule ^([0-9a-zA-Z]+)/?$ /index.php?v=$1 [L]