Rewrite only valid url using htaccess - php

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.

Related

.htaccess can't read url with %20

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.

How to handle special characters like & and / in .htaccess rules?

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]

URL Rewrite with htaccess and PHP

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.

Trouble with URL rewriting in .htaccess

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

Htaccess Rewrite Rule Conditional

I am a complete noob when it comes to the .htaccess file... What I'm trying to do is check to see if a GET variable exists, ?si=10, and pass that onto the rewrite, but only if it exists.
RewriteRule ^user/([^/\.]+)/?$ profile/index.php?name=$1
This way when I have say website.com/user/Username/ it goes to, on the server, website.com/profile/?name=Username instead. But when I add do website.com/user/Account/?si=10, the server isn't passing the si variable onto the actually loaded page. Any idea how I would do this? Sorry if I worded this badly..
Try the QSA flag
RewriteRule ^user/([^/\.]+)/?$ profile/index.php?name=$1 [QSA]
From the manual...
When the replacement URI contains a
query string, the default behavior of
RewriteRule is to discard the existing
query string, and replace it with the
newly generated one. Using the [QSA]
flag causes the query strings to be
combined.
You can do it like this:
RewriteCond %{QUERY_STRING} ^si=([0-9]+)$ // replace regex as neccesary
RewriteRule ^user/([^/\.]+)/?$ profile/index.php?name=$1&si=%1
The percentage sign % brings in the match or matches from the rewriteCond(s) and you use the dollar sign as normal for the matches from the rewriteRule. This gives you full control of your query vars, but yes, QSA is simpler.

Categories