I'm trying to match special characters in my .htaccess file for so an id value can return a page matching the correct id value.
In my MySQL field the text is: Thelma & Louise
Before the rewrite rule page address looked like this with all the property data populating the page www.site.com/movie.php?id=Thelma+%26+Louise
My RewriteRule ^movie/([A-Za-z0-9_-\s]+)/?$ /movie.php?id=$1
The url comes out like this but with a page not found error
www.site.com/movie/Thelma+%26+Louise
How can I properly match the ascii characters so that the page is displayed.
Thanks for any help!
The URI gets decoded before it gets run through any of the rewrite rules, so you need to match against a space and an ampersand &. Your pattern, ([A-Za-z0-9_-\s]+) needs to account for those symbols:
RewriteRule ^movie/([A-Za-z0-9_&+-\s]+)/?$ /movie.php?id=$1 [L,B]
Additionally, you need to use the B flag so that the grouped match $1 gets propery encoded in the query string.
First of all i wouldn't use any ascii characters in my url. Maybe try trimming them so you have thelma-louise or thelma+louise. But thats my personal experience.
Second, you rewrite to your ID with the name of the movie. Can't you do it like this:
movie/([0-9a-zA-Z-]+)-([0-9]+) movie.php?id=$2 so its looks something like movie/thelma-louise-101 Lots of movies have the same name. And now you know that IDS are at least an INT. Don't forget to check in PHP offcourse.
Your second rule probably doesn't match. You could try to put all possible characters in your regex, but it is probably better to match all characters, but the characters you don't want to match. In this case that would probably be the / character:
RewriteRule ^movie/([^/]+)/?$ /movie.php?id=$1
Please note that you still have to make sure you are not creating an infinite loop.
Related
I dont know much about htaccess and haven't found a solution for this issue.
I want that if a user opens example.com/#username/posts a php file is displayed and the username and page type (e.g. /posts/) is availiable as a get argument.
I already found this: RewriteRule "^user/([A-Z a-z 0-9_\-.]{1,})((/\w+)+|/?)$" "/account/profile.php?name=$1" but this isn't my usecase because its example.com/user/username
Do you know how to realise example.com/#username/posts orexample.com/#username/images with my htaccess or other stuff?
Thanks a lot, I am a newbie in this area :D
Have a great sunday!
Try:
RewriteRule "^#([\w.]+)\/(.+)$" "/account/profile.php?name=$1&page=$2"
^ and $ make it so that the expression in-between must try to match everything, not a sub-string.
([\w.]+) matches and captures uppercase, lowercase, 0 to 9, underscore (_), hyphen and dot (the dot might not be required in your case, in which case you can change it to (\w+)). + makes it greedy (matches as much as possible).
(.+) matches and captures as much a possible.
I know there are many similar out there but I did not find a working solution for me.
I have incoming URLs like: https://example.com/123-frank-street
and want them to rewrite to: https://example.com/street/index.php?name=123-frank-street
I tried dozens of versions and my closest is the following
RewriteRule ^[0-9]+([A-Za-z0-9-]+)/?$ /street/index_test.php?street=$1 [NC,L]
This only rewrites to: https://example.com/street/index.php?name=-frank-street
The 123 is missing and gets somehow not forwarded. only the rest!
What I am missing here or doing wrong?
thx in advance
Rewrite rules are really quite simple once you understand their structure:
On the left is a regular expression which determines which URLs from the browser the rule should match
Inside that regular expression, you can "capture" parts of the URL with parentheses ()
Next, is the URL you want to serve instead; it can include parts that you captured, using numbered placeholders $1, $2, etc. It's entirely up to you where you put these, and Apache won't guess
Finally, there are flags which act as options; in your example, you're using "NC" for "Not Case-sensitive", and "L" for "Last rule, stop here if this matches"
In your example, the pattern you are matching is ^[0-9]+[A-Za-z0-9-]+/?$, which is "from start of URL, 1 or more digits, one or more letters/digits/hyphens, 0 or 1 trailing slash, end of URL".
The only part you're capturing is ([A-Za-z0-9-]+), the "one or more letters/digits/hyphens" part; so that is being put into $1. So the rest of the URL is being "discarded" simply because you haven't told Apache you want to put it anywhere.
If you want to capture other parts, just move the parentheses, or add more. For instance, if you write ^([0-9]+)([A-Za-z0-9-]+)/?$ then $1 will contain the "one or more digits" part, and $2 will contain the "one or more letters/digits/hyphens" part.
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
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]
I've been using this line in a routes file:
$route['^(?!home|members).*'] = "pages/view/$0";
The string in the array on the left of the expression ( ^(?!home|members).* ) is what I'm trying to figure out.
Basically any url that is not:
/home or /home/ or /members or /members/ should be true. The problem I have is if the url is something like /home-asdf. This counts as being in my list of excluded urls (which in my example only has 'home' and 'members'.
Ideas on how to fix this?
Try this modification:
^(?!(home|members)([/?]|$)).*
This filters out URLs beginning with home or members only if those names are immediately followed by a slash or question mark ([/?]), or the end of the string ($).
http://www.regular-expressions.info/
The dot . operator matches all characters. The * operator means the previous pattern will be repeated 0 or more times. That means the end of your route matches any character any number of times after the word home or members. If you only want to match one or zero slashes, then change .* to /?.
As an aside, I use this all the time and it works wonders: http://www.rubular.com/
Its predominantly for ruby but works well when working out general regex for php etc too.