In my website, I am trying to rewrite few of my URLs as bellow:
This URL
http://example.com/news.php?newstype=newsletter&newsid=1123&newstitle=my news title
int to:
http://example.com/newsletter/1123/my news title
My rewrite rule in .htaccess looks like as shown below:
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteRule ^(.*)/(.*)/(.*)/?$ news.php?newstype=$1&newsid=$2&newstitle=$3 [L,QSA,NC,B]
My <a> tag is looks this:
$seoUrl = "$urlNewsType/$newsid/$urlNewsTitle/";
$seoUrl = urldecode($seoUrl);
$seoUrl = html_escape($seoUrl, 'UTF-8');
Read More
These every things are working for me. But my question is, I need to get these query string parameters separately from URL into PHP. Problem is I can't get these prameters as I do usually.
In php, This is the output of:
echo '<pre>',print_r($_GET).'</pre>';
Array
(
[newstype] => news/1114
[newsid] => Presentation: Heath+Day+in+2018
[newstitle] =>
)
Can anybody tell me, what is the wrong which I have done in this regard?
The regex pattern (.*) is greedy. It matches the full requested path. Try ([^/]+) instead :
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ news.php?newstype=$1&newsid=$2&newstitle=$3 [L,QSA,NC,B]
Related
I hope you can help me.
I have the problem that I would like to mask URLs from another (not on my server) Domain
<img src="https://images2.externaldomain.com/?w=200&h=200&bg=white&trim=5&t=letterbox&url=ssl%3Ai.otto.de%2Fi%2Fotto%2F60393181-9d98-5221-b0ab-35d38cd2aee2.jpg%3F%24Preset_Retargeting_640%24&feedId=62797&k=bd10410eeddf3ad8980f6e543edf4455110cb164">
to a masked URL like
<img src="https://mysubdomain.mydomain.com/productimages/bd10410eeddf3ad8980f6e543edf4455110cb164.jpg">
The image ID should the ID at the end k=
The problem is that I need a wildcard solution because I have a lot images with these URLs and only https://images2.externaldomain.com is stable. Every image has an ID (k=) in the source URL.
which RewriteRule or Condition could work?
many thank to every hint!
I added the snippet into my .htaccess without effect on my html. the sources of the images look exactly the same. how can I mask them that it look like image source of my server?
the hint of MrWhite was great and it works on https://htaccess.madewithlove.com/ but not on my site - what did I wrong?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)k=([\da-f]+)
RewriteRule ^$ productimages/%1.jpg [QSD,L]
</IfModule>
My images are look like:
<img src="https://images2.externaldomain.com/?w=200&h=200&bg=white&trim=5&t=letterbox&url=ssl%3Ai.otto.de%2Fi%2Fotto%2F60393181-9d98-5221-b0ab-35d38cd2aee2.jpg%3F%24Preset_Retargeting_640%24&feedId=62797&k=bd10410eeddf3ad8980f6e543edf4455110cb164">
or
<img src="https://images2.externaldomain.com/?w=200&h=200&bg=white&trim=5&t=letterbox&url=ssl%3Ai.otto.de%2Fi%2Fotto%2F06a05fc1-0554-5463-a8cf-6092a79214f0.jpg%3F%24Preset_Retargeting_640%24&feedId=56149&k=d27dd6627f26f5350af6afcec2c2afd71500c218">
in HTML code. Images are external on another server.
I would like to mask or cloak them with htts://mysubdomain.mydomain.com/productimages/d27dd6627f26f5350af6afcec2c2afd71500c218.jpg
To internally rewrite a URL of the form:
/?w=200&h=200&bg=white&trim=5&t=letterbox&url=ssl%3Ai.test.com%2Fi%test%2F37b3895d-f743-4572-9017-6725903fef30.jpg%3F%24Preset_Retargeting_640%24&feedId=62797&k=4c8370f2e926de654b1f0a08530bc6065e6a80d3
to
/folder/4c8370f2e926de654b1f0a08530bc6065e6a80d3.jpg
In other words, capturing the value of the k URL parameter in the originally requested URL and use this as the file basename in the URL being rewritten to, then you can do something like the following using mod_rewrite near the top of the root .htaccess file (the order of directives can be important):
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)k=([\da-f]+)
RewriteRule ^$ folder/%1.jpg [QSD,L]
This ignores all other URL parameters and looks only for the k URL param. This param can occur anywhere in the query string.
([\da-f]+) - This regex matches the value of the k URL param, which looks like a hexadecimal string. In your example this is 40 characters long - if it is always 40 characters long then this could be limited in the regex as well. eg ([\da-f]{40}) - to only match hex strings that are 40 characters long.
Optionally, you can also test whether the target URL exists as a file before rewriting the request. For example, add the following condition (RewriteCond directive) after the first:
:
RewriteCond %{DOCUMENT_ROOT}/folder/%1.jpg -f
:
How can I rewrite url from this:
https://www.domain.com/blog/category/sub-category-name
to this:
https://www.domain.com/blog/category/?category=sub-category-name
also it must work with others parameters like:
https://www.domain.com/blog/category/sub-category-name?param=value => https://www.domain.com/blog/category/?category=sub-category-name¶m=value
or
https://www.domain.com/blog/category/sub-category-name#hash => https://www.domain.com/blog/category/?category=sub-category-name#hash
I don't understand this rewrite syntax so any advice is valuable.
Thanks
You can use this rule :
RewriteEngine on
RewriteRule ^blog/([^/]+)/([^/]+)/?$ /blog/$1/?$1=$2 [QSA,NC,L]
Additional Query perameters are automatically appended to the target path by QSA flag.
I need to append all url's from a folder with a query param using Apache rewrite rule.
For this all url's starting with /abc/def/xyz/ the url should be appended with ?v=2
For example, /abc/def/xyz/folder/test.pdf should become /abc/def/xyz/folder/test.pdf?v=2
I tried with RewriteRule /abc/def/xyz(.*) /abc/def/xyz/$1?v=2 but it is not working.
I think you've got it only slightly wrong (start of the pattern), try this:
RewriteRule ^abc/def/xyz/(.*) /abc/def/xyz/$1?v=2
Tested here:
http://martinmelin.se/rewrite-rule-tester/
I have a php script that relies on a $_GET variable called "serial." Normally the URL would look like this:
http://www.example.com/script.php?serial=XXXXXX
However, the person making the QR codes thought that the URL was too long and has already finished making QR codes that point to this URL:
http://www.example.com/script.php?XXXXXX
The part they left out was the "serial=", so PHP can't $_GET the variable anymore.
Is it possible to redirect using .htaccess to add the "serial=" back in?
Any help would be appreciated.
Thanks!
Try:
RewriteEngine On
RewriteCond %{QUERY_STRING} !^serial=
RewriteRule ^script\.php$ /script.php?serial=%{QUERY_STRING} [L]
If you want to redirect the browser, add a R to the inside of the square brackets so that it looks like [L,R].
I'm working on a Business Directory Website. What I'd like to do is transform the URL String of :
http://www.website.co.uk/business/results.php?category_id=11
To http://www.website.co.uk/business/Financial & Legal
How do I achieve this? Not too sure on the Rewrite Rules etc I am to use.
Thanks for any help, in advance.
I am now trying jut a test on a local host.
The HTML Code is as follows :
<h1>This is the PHP file.</h1>
Link Here
So all I want is the URL to be http://localhost/mod_rewrite/index.php/category
rather than : http://localhost/mod_rewrite/index.php?url=category
The .htaccess file is as follows (But doesnt appear to work)??
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ /index.php?url=$1 [L]
Well,
in general you don't use ampersands in URLs.
They will be replaced by "%26" when you try to call them as a link.
But a general approach towards your question could look like that:
RewriteEngine on
RewriteRule ^business/Financial.*?Legal$ results.php?category_id=11 [L]