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.
Related
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.
I'm trying to write a regexp.
some background info: I am try to see if the REQUEST_URI of my website's URL contains another URL. like these:
http://mywebsite.com/google.com/search=xyz
However, the url wont always contain the 'http' or the 'www'. so the pattern should also match strings like:
http://mywebsite.com/yahoo.org/search=xyz
http://mywebsite.com/www.yahoo.org/search=xyz
http://mywebsite.com/msn.co.uk'
http://mywebsite.com/http://msn.co.uk'
there are a bunch of regexps out there to match urls but none I have found do an optional match on the http and www.
i'm wondering if the pattern to match could be something like:
^([a-z]).(com|ca|org|etc)(.)
I thought maybe another option was to perhaps just match any string that had a dot (.) in it. (as the other REQUEST_URI's in my application typically won't contain dots)
Does this make sense to anyone?
I'd really appreciate some help with this its been blocking my project for weeks.
Thanks you very much
-Tim
I suggest using a simple approach, essentially building on what you said, just anything with a dot in it, but working with the forward slashes too. To capture everything and not miss unusual URLs. So something like:
^((?:https?:\/\/)?[^./]+(?:\.[^./]+)+(?:\/.*)?)$
It reads as:
optional http:// or https://
non-dot-or-forward-slash characters
one or more sets of a dot followed by non-dot-or-forward-slash characters
optional forward slash and anything after it
Capturing the whole thing to the first grouping.
It would match, for example:
nic.uk
nic.uk/
http://nic.uk
http://nic.uk/
https://example.com/test/?a=bcd
Verifying they are valid URLs is another story! It would also match:
index.php
It would not match:
directory/index.php
The minimal match is basically something.something, with no forward slash in it, unless it comes at least one character past the dot. So just be sure not to use that format for anything else.
To match an optional part, you use a question mark ?, see Optional Items.
For example to match an optional www., capture the domain and the search term, the regular expression could be
(www\.)?(.+?)/search=(.+)
Although, the question mark in .+? is a non-greedy quantifier, see http://www.regular-expressions.info/repeat.html.
You might try starting your regex with
^(http://)?(www\.)?
And then the rules to match the rest of a URL.
$re = '/http:\/\/mywebsite\.com\/((?:http:\/\/)?[0-9A-Za-z]+(?:-+[0-9A-Za-z]+)*(?:\.[0-9A-Za-z]+(?:-+[0-9A-Za-z]+)*)+(?:\/.*)?)/';
https://regex101.com/r/x6vUvp/1
Obeys the DNS rule that hyphens must be surrounded. Replace http with https? to allow https URLs as well.
According to the list of TLDs at Wikipedia there are at least 1519 of them and it's not constant so you may want to give the domain its own capture group so it can be verified with an online API or a file listing them all.
Here is my two cents :
$regex = "/http:\/\/mywebsite\.com\/((http:\/\/|www\.)?[a-z]*(\.org|\.co\.uk|\.com).*)/";
See the working exemple
But I'm sure you can do better !
Hope it helps.
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'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.
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.