I have tried to GET a string from a URL, for instance
'www.example.com/store/samsung-A5-2016'
I want to get 'samsung-A5-2016' from the link with the $_GET method, but all I 'get' is "Samsung".
I want to get the whole string 'samsung-A5-2016'.
You need to add the - at the beginning or the end of your character group so that it does not define a range:
RewriteRule ^store/([-0-9a-zA-Z]+) store.php?d=$1 [NC,L]
^ Here you can add the - sign
Note that this only allows for the - sign in your urls, if you want others, you need to add these separately too.
Please below rule for .htaccess
Because of currently your regex of .htaccess rule is not allowing '-' it gives string before the '-' of entire querystring value.
RewriteEngine on
RewriteRule ^store/(.*) store.php?d=$1 [L]
Related
I have the following re-write rule:
RewriteRule ^([\w-]+)$ modules.php?mod_name=$1
It allows me to open a url like:
mydomain.com/settings
This calls the php file in the following format:
modules.php?mod_name=settings
However, I need to call my php script with mod_name values that contain a path e.g:
modules.php?mod_name=settings/preferences.php?id=1103
I would like the re-write rule to be able to accept the above in the following format:
mydomain.com/settings/preferences/1103
Any ideas how this can be done?
RewriteRule ^([\w-]+)$ modules.php?mod_name=$1
Your \w matches word characters ([a-z] [A-Z] [0-9]) and dashes (-) (thus not the /).
To include the /, you need something like ^([\w-/]+)$
I'm trying to add a query parameter to the end of all URLs on a site but that value needs to be dynamic.
RewriteCond %{QUERY_STRING} !(^|&)testing=true(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}?testing=true [L,QSA,R]
This answer (code above) is very close to what I need, but I need 'true' to change based on the user provided value. An example would be various campaign ID's
http://domain.com/?campaign=32324
And when a user clicks on a link from this page, those links should append the ?campaign=32324
http://domain.com/somepage/?campaign=43454
If you want to append a query string to a URL, I believe this is what you're looking for.
Of course your parameter and/or its value can be populated in any number of ways. It can be passed from a previous page and populated by $_GET['something'] or provided by a DB query and populated from the results.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^([A-Za-z0-9_-]+)$ your-page.php?your-parameter=$1 [NC,L] # Handle query string with no slash
RewriteRule ^([A-Za-z0-9_-]+)/$ your-page.php?your-parameter=$1 [NC,L] # Handle query string with a slash
The [A-Za-z0-9_-] portion allows a parameter value to include upper & lower case letters, numbers, underscore and hyphen. If all you want are numbers (as in your examples) simply remove everything between the square brackets except 0-9 like so... [0-9]
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]
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.
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