in .htaccess imposibla use space, non-Latin characters and etc. ?
For example how, fix this:
RewriteRule ^/(.*)/new user/(.*)$ /page/?id=$1 [QSA,L,E]
this decision is not me:
... /new_user/ ...
Thanks
You have to escape the reserved characters for URIs with their respective character codes in percent-encoding, e. g. %20 for a space.
See http://en.wikipedia.org/wiki/Percent-encoding for more details.
OK .. signs allowed in url:
http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
and in htaccess:
RewriteRule ^(([0-9]+)-)?new_user(-([0-9]+))?/?$ new_user.php?page=$2&id=$4 [L]
change www.blabla.com/10-new_user-5
to www.blabla.com/new_user.php?page=10&id=5
The URL is automatically decoded before your rules operate on it, so you can test against literal characters, e.g. français\ español (note that you need to escape literal spaces with a backslash, unless the pattern is quoted).
However, based on your example, I think your actual concern is being able to do something like this:
RewriteRule ^([^/]+)/[^/]+/.*$ /page/?id=$1 [QSA,L]
# ^---^
# it doesn't matter what the user name is, as long as it
# doesn't contain a slash
Related
I have to redirect urls such as "example.com/example/(justsometext)/id-123" into something more elegant like "example.com/example/id-123" so I want to get rid of parentheses. How can I do it in a way (justsometext) isn't considered as a % variable (%1)?
Thank you!
Romano
I searched for an answer everywhere but didn't manage to find any suitable solution.
You could do something like this near the top of the root .htaccess file using mod_rewrite:
RewriteEngine On
RewriteRule ^(.+)/\([^/]+\)(.*) /$1$2 [R=302,L]
(You do not need to repeat the RewriteEngine directive if this already occurs later in the file, as I expect it does.)
Note that to match literal parentheses in the regex, they are backslash-escaped to negate their special meaning (as a group delimiter). ie. \( and \) respectively.
The backreferences $1 and $2 contain the parts of the URL-path that surround the part to be removed. ie. $1 contains everything up to (but not including) the slash that precedes the parenthesised section.
This does, however, make various assumptions:
There is always at least one path segment before the path segment that needs to be removed (ie. the one that contains (justsometext)). This satisfies your example: /example/(justsometext).
justsometext can be any characters enclosed in literal parentheses, except other slashes.
(justsometext) occupies a complete path segment, prefixed with a slash.
Songs that contain a "#" in the track title give me a 404 error while trying to download from my site. How to fix this?
This is my current .htaccess code:
RewriteRule ^download/([^/]*)-([^/]*).mp3$ download.php?download=$2&pl=$1
The # is never reaching apache. It is a jump-marker and everything in the url after # is not sent to the webserver. The javascript:document.location.hash is jumping to an anchor in the page. You can escape the # with %23 which then should reach your webserver even without htaccess. As you use php, you can use htmlentities($filename) or urlencode($filename) to fix it during output.
From the
Apache manual
By default the special characters such as ?, #will be converted to their hexcode.Using the [NE] flag prevents that from happening.
RewriteRule ^download/([^/]*)-([^/]*)\.mp3$ download.php?download=$2&pl=$1 [NE,QSA,NC,L]
The above example will redirect /download-foo-bar.mp3
to download.php?download=bar&pl=bar#1 . Omitting the [NE] flag will result in the #
being converted to its hexcode %23., which will then result in 404 not found error condition.
And in RewriteRule pattern ,dot (.) is a special characters, it matches any characters. Escape it using a backslash if you want to match a literal dot(.).
I am only just starting to learn how to rewrite urls with the .htaccess file.
How would I change:
http://www.url.net/games/game_one.php
into this:
http://www.url.net/games/game-one/
This is what I have been trying
RewriteRule ^/games/game-one.php ^/games/game-one/ [NC,L]
If you want people to use /games/game-one/ explicitly, you have to rewrite so that it requests /game/game-one.php. So the opposite way around than you have it in your question.
RewriteEngine On
RewriteRule ^games/game-one/$ /games/game-one.php
If you want to rewrite other URL's too, then you'd need to use a technique similar to the prior answer.
Try this:
RewriteRule ^(/games/game-one)\.php $1/
What that says is match anything starting with /games/game-one and remember the first part of that match, then replace it with the first part (capturing group in regex speak), and a slash character. Note that to match a period character you must precede it with a \ since . is a special character that means "any character" (at least if you care to avoid matching any character).
I want to create my url structure like::
facebook i.e. facebook.com/?pageid=122
For which I am using htaccess mod rewrite as:
RewriteRule ^([a-zA-Z_\-]+)/?([a-zA-Z0-9\-=&_#]*)$ /$1.php?$2 [QSA,L]
so I may translate pages like site.com/home/?pageid=22 into site.com/home.php?pageid=22
The code above works fine except that if I try to add dot like
RewriteRule ^([a-zA-Z_\-]+)/?([a-zA-Z0-9\-=&_#\.]*)$ /$1.php?$2 [QSA,L]
The .htaccess breaks. I need dot so I may pass emails too i.e.
site.com/home/?email=sohaib.dmc#gmail.com
Please help
Try to remove the backslash before the dot. Since it's not considered as a special character inside brackets in a POSIX regular expression.
You need scape the question mark because it's a special character:
RewriteRule ^([a-zA-Z_\-]+)/\?([a-zA-Z0-9\-=&_#\.]*)$ /$1.php?$2 [QSA,L]
The question mark makes the preceding token in the regular expression optional. E.g.: colou?r matches both colour and color.
http://www.regular-expressions.info/optional.html
I'm trying to make a friendly URL through RewriteRule but it keeps ignoring # as part of the variable value.
The line on .htaccess is as simple as this
RewriteRule ^key/(.+)/$ index.php?key=$1
and the requested URL is
http://www.example.com/key/c%23/
but I'm only getting c as get variable and not c%23.
What exactly am I doing wrong?
Finally after some digging, I managed to pull this off.
It just needs the B flag on RewriteRule to escape non-alphanumeric characters such as #
RewriteRule ^key/(.+)/$ index.php?key=$1 [B]
%23 is a hash symbol (#), so it (and anything after it) doesn't actually get parsed by mod_rewrite. The actual URL therefore is http://www.foo.com/key/c, without any %23. Other dash-codes work fine, though.
%23 is a hash mark (#). I'm guessing the browser is interpreting the hash as an anchor and not passing it on to the server. For instance, if you user http://www.foo.com/key/c%20/ you'll get "c[space]".