This is how my htaccess file look right now:
RewriteEngine On
RewriteRule ^([\sa-zA-Z0-9_-]+)$ view.php?folder=$1
RewriteRule ^([\sa-zA-Z0-9_-]+)/$ view.php?folder=$1
RewriteRule ^([\sa-zA-Z0-9_-]+)\.html$ view.php?page=$1
It accepts the url as:
http://localhost/new.html to view.php?page=new
http://localhost/something to view.php?folder=something
http://localhost/something/ to view.php?folder=something
but it is not working for url as:
http://localhost/something%20else
http://localhost/something%20else/
it should be view.php?folder=something%20else
According to this answer you should be using a url rewrite flag on your htaccess rewrite.
(shameless quote of link to follow:)
Try adding the B rewrite flag. This flag tells mod_rewrite to escape
backreferences, the documentation says
this:
_rewrite has to unescape URLs before mapping them, so backreferences will be unescaped at the time they are applied. Using the B flag,
non-alphanumeric characters in backreferences will be escaped.
Related
I have the following htaccess rule to rewrite my URL
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^app/(.*)/(.*)/(.*)$ app/index.php?app_released=$1&app_name=$2&app_version=$3 [L,NC]
</IfModule>
Which rewrites to app/3-25-2018/name/version that's fine, but when user tries to put more slashes and some random values after the slash for example app/3-25-2018/name/version/something/else/here it should redirect them to my custom 404 page but I'm receiving undefined app_released error by PHP because, it's not able to read the GET variable properly. How can I fix this?
The problem lies in the regular expression you use and particularly (.*), which matches all characters including /. Instead, supplant (.*) with ([^\/]*) which matches all characters besides /.
Correct Regex:
^app\/([^\/]*)\/([^\/]*)\/([^\/]*)$
See the above regular expression in action in the following examples:
matching app/3-25-2018/name/version - here.
not matching app/3-25-2018/name/version/something/else/here - here.
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.
I have a URL: search/?word=asdf and want to redirect to: search/word/asdf/ and running internally: ?cmd=search&word=asdf
This so you can get the PHP $ _GET ['cmd'] and $ _GET ['word'].
How to do it in htaccess?
EDIT:
My .htaccess now is:
RewriteRule search(.*) %{HTTP_REFERER}cmd/search$1
RewriteRule cmd/search/?key-word=(.*) %{HTTP_REFERER}cmd/search/key-word/$1
But this not working. The new URL ever is:
localhost/bruc/sandbox/electrolux/trunk/cmd/search/?key-word=asdf
But it should be: localhost/bruc/sandbox/electrolux/trunk/cmd/search/key-word/asdf
So, I redirect this correct URL to: localhost/bruc/sandbox/electrolux/trunk/?cmd=search&key-word=asdf
But not working fine! Try, my approach here: http://htaccess.madewithlove.be/
Try RewriteRule ^([^/]*)/word/([^/]*)$ /?cmd=$1&word=$2 [L]. I believe that will accomplish your goal.
Try this :
RewriteEngine on
RewriteRule ^search/word/(.*)$ /?cmd=search&word=$1 [L]
Check this.
RewriteEngine on
RewriteRule ^([^/]+)/([^/]+)/([^/]+) /?cmd=$1&word=$2 [L]
There are three parts to this:
RewriteRule specifies that this is a rule for rewriting (as opposed to a condition or some other directive). The command is to rewrite part 2 into part 3.
This part is a regex, and the rule will be run only if the URL matches this regex. In this case, it says - look for the beginning of the string, then a bunch of non-slash characters, then a slash, then another bunch of non-slash characters. then again bunch of non-slash characters, then a slash, then another bunch of non-slash characters. The parentheses mean the parts within the parentheses will be stored for future reference.
Finally, this part says to rewrite the given URL in this format. $1 and $2 refer to the parts that were captured and stored.
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
I'm trying to forward example.com/signup and example.com/signup/ (with trailing slash) to example.com/signup.php
I wrote following to .htaccess and it works for example.com/signup/ but doesn't work without trailing slash... How can I solve the problem?
RewriteEngine On
RewriteRule ^question/(.*)-(.*) /question.php?qid=$1
RewriteRule ^signup/ /signup.php
RewriteEngine On
RewriteRule ^signup/?$ /signup.php
The question mark makes the slash optional, and the dollar sign means end-of-line, so nothing can follow. You can remove the $ if you like.
You could also allow arbitrary query strings at the end of the URL with something like:
RewriteRule ^signup/?(\?.*)?$ /signup.php$1
That would allow you to pass any query parameters on to your PHP script. For example the URL http://www.example.com/signup?destination=/front-page would be redirected to http://www.example.com/signup.php?destination=/front-page.
Make the slash optional with the ? quantifier:
RewriteRule ^signup/?$ /signup.php
But I recommend you to just use one notation and redirect the other one.
Put a question mark after the slash
RewriteRule ^signup/? /signup.php
? = optional single character in regex matches