So I'm trying to pass a PHP urlencode() variable through a mod_rewrite rule, but I can't seem to get it working correctly.
Currently I'm passing this sort of thing:
/test/abc%40test.co.uk
The # symbol replaced with the &40 in the urlencode.
Through this rule:
RewriteRule ^test/([-_.%A-Za-z0-9]+)/?$ test.php?variable=$1
As far as I'm aware this should allow the % symbol through; why isn't it working? Am I missing something obvious?
The URL is decoded before it is sent through the rewrite engine, so you need to match against #, and not the encoded string. Try:
RewriteRule ^test/([-_.#A-Za-z0-9]+)/?$ test.php?variable=$1
Related
In my .htaccess file I have defined following rule,
RewriteRule ^([-0-9a-zA-Z]+) search.php?id=$1
The above rule works fine if I am browsing http://example.com/abcd
I need to use the symbols & % - / in the url like: http://example.com/ab&cd
What changes have to be made to the rule for this to work?
No idea how that rule is working for you. First, it loops. Second, there is no capture groups for $2 and $3, but it doesn't matter because $1 is always "search" anyways. I'm assuming you've pasted a partial snippet of a rule that you have that works.
The reason why &, %, or / isn't being matched is because your regex says:
[-0-9a-zA-Z]+
which means: one or more letters, numbers, or a dash. So no &, %, or /. So you can add those into the square brackets:
RewriteRule ^([-0-9a-zA-Z/%&]+) search.php?id=$1&ff=$2&ffid=$3
However, keep in mind that the URI is decoded before any rules get applied. This means if the URI looks like:
/foo%28bar
You don't need to match against %, because the URI gets decoded into:
/foo(bar
and you need to match against (. A better option may to just match against every except dots:
RewriteRule ^([^.]+) search.php?id=$1&ff=$2&ffid=$3
or whatever you don't want in your match.
Try:
RewriteRule ^([^.]+)$ search.php?id=$1 [B]
The difference here is the $ to bound the match to the end of the URI, and the B flag ensures the & gets encoded.
You need to URL encode 'ab&cd' using urlencode
so that & gets turned into %26.
After this, in search.php you'll need to account for it and decode it, using urldecode.
Do the URL like this:
http://example.com/id/ff/ffid
and write your rule around that. Also, why do you need 3 ID parameters? Couldn't you just lookup the other 2 using the first one?
Do with your url
RewriteRule ^directory/([^/.]+)$ /searchpage.php?search_keywords=$1 [L]
OK. I have this regular expression in my .htaccess file to rewrite / anything alphanumeric to index.php?id=alphanumeric string.
RewriteEngine on
RewriteRule ^([A-Z-a-z0-9]+)$ index.php?id=$1
the problem i'm having is that when some other variables get added to that string, everything stops working.
For example:
www.someaddress.com/ABCDEFGH works fine.
www.someaddress.com/ABCDEFGH&othervariable=123 fails.
I know my alpha numeric string is always 8 characters. is there a way to make the regular expression only match 8 and leave the rest of the string?
Thanks in advance.
Something like that should do the trick. You want to remove the $ to let it ignore the rest of the variables.
RewriteRule ^([A-Z-a-z0-9]{8}) index.php?id=$1
try this:
RewriteRule ^([A-Za-z0-9]{8}) index.php?id=$1
Please note the - character between Z and a is not required
Right now i am using the given below regex
RewriteRule ^(love-wallpaper.php|quotes-wallpaper.php)+ wallpapers.php
to rewrite love-wallpaper.php and quotes-wallpaper.php to wallpapers.php
I want above rule should not rewrite wrong url like love-wallpaper.php&anything
but should rewrite to url like love-wallpaper.php?anything to wallpapers.php
How can this be done?
Example :
Urls like love-wallpaper.php#asdfasdfasdf should not rewrite to wallpapers.php
but url like love-wallpaper.php?aasdbfsbdf should redirect to wallpapers.php
Thanks
You're probably looking for the following:
RewriteRule ^(love|quotes)\-wallpaper\.php$ wallpapers.php [QSA]
I've also escaped the - character, as it's reserved in regex.
Explanation of regex/htaccess:
^ - "starts with"
(love|quotes) - the string "love" or the string "quotes"
\-wallpaper\.php - the string "-wallpaper.php" with - and . escaped.
$ - "end must be here"
[QSA] sends your URL parameters (url.php?my=parameter) onto the wallpapers.php page.
You cannot decide not to match url.php#hash as URL hashes cannot be used in .htaccess matching, as they're never sent to the server - see Redirect URL with hash using .htaccess file
It's not very clear to me what you are looking for, But If you want to match for a valid query string after quotes-wallpaper.php use below rule
RewriteRule ^(love-wallpaper.php|quotes-wallpaper.php)+(?![\w&#\$!\(\)]+) wallpapers.php
It will not match &,#,$ etc after wallpaper.php.You can put more symbols in regex character class[] if you don't want them to match and rewrite and vice-versa.
Note: This will entirely ignore any query string in passed in url.
Alright, I give up. I just can't quite wrap my mind around apache rewrites, I've looked through a lot of the stackoverflow suggestions and none seems to make sense to me.
So, I have a script that current renders content based on www.example.com/index.php?article=some-article-name
But, I want the user to think that page is www.example.com/section/some-article-name
I've tried using stuff like
# Turn on URL rewriting
RewriteEngine On
RewriteRule ^/section/([a-zA-Z0-9\-]+)$ index.php?article=$1
I discovered the answer thanks to the direction of all of these folks.
RewriteRule ^section/([a-zA-Z0-9]+)$ test.php?article=$1
RewriteRule ^section/([a-zA-Z0-9]+)/$ test.php?article=$1
You need both to handle 2 different types of requests, ones with a / at the end and those that don't.
You may want a simpler rule like.
RewriteRule ^/section/(.*) index.php?article=$1
A name like some-article-name will fail because you won't match the hyphen. If you want a limited regex try something like:
RewriteRule ^/section/([-_.a-zA-Z0-9]+)$ index.php?article=$1
This will match ASCII alphanumeric characters along with punctuation most likley to be in the name.
Either of these rules will fail if you have parameters on the incoming request.
Your RegEx is SO close. You have a capturing group ([a-zA-Z0-9]+) that is looking for one or more lower case letters, upper case letters and/or numbers, but what it isn't looking for is a hyphen. Try this:
RewriteRule ^/section/([a-zA-Z0-9\-]+)$ index.php?article=$1
Because the hyphen has special significance in Regular Expressions you need to escape it.
My .htaccess file looks like this:
RewriteEngine On
RewriteRule ^articles/(\d+)*$ ./articles.php?id=$1
So, if the URL foo.com/articles/123 is requested, control is transferred to articles.php?id=123.
However, if the requested URL is:
foo.com/articles/123/
or
foo.com/articles/123/whatever
I get a "404 Not Found" response.
I would like to call articles.php?id=123 in all these cases. So, if the URL starts with foo.com/articles/[digits]... no matter what other characters follow the digits, I would like to execute articles.php?id=[digits]. (The rest of the URL is discarded.)
How do I have to change the regular expression in order to achieve this?
Just don't look for the end:
RewriteRule ^articles/(\d+) ./articles.php?id=$1
You do need to allow the trailing / with:
RewriteRule ^articles/(\d+)/?$
The \d+ will only match decimals. And the $ would disallow matches beyond the end.
If you also need trailing identifiers, then you need to allow them too. Then it might be best to make the match unspecific:
RewriteRule ^articles/(.+)$
Here .+ matches virtually anything.
But if you want to keep the numeric id separate then combine those two options:
RewriteRule ^articles/(\d+)(/.*)?$ ./articles.php?id=$1