.htaccess rewrite url - convert from nice URL back to GET parameter - apache

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&param=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.

Related

GET query string parameters from .htaccess rewrite URL in PHP

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]

Rewrite to lang parameter

I'm really bad at htaccess, I can't figure this out.
I have the following file:
/site/.htaccess
I would like to create a rewrite that would rewrite:
/site/en/home/
to:
/site/home/?lang=en
and (var is just an example, it should work with any query string):
/site/en/home/?var=12345
to:
/site/home/?var=12345&lang=en
How can I accomplish this? I've been at it for 1 hour and I can't get it to work.
You can use this in /site/.htaccess
RewriteEngine on
RewriteRule ^en/(.+)/?$ /site/$1/?lang=en [QSA,L]
QSA QueryStringAppand flag automatically appends the addtional queryString to the target.

Apache rewrite rule to add query string to a url from a folder

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/

Apache rewrite rule for optional query parameter

Even after lengthy perusal, the Apache rewriterule documentation continues to confound me.
Currently I am using the following .htaccess URL rewrite rule:
RewriteRule ^([_0-9a-zA-Z-]+)\.html$ index.php?a=p&p=$1 [nc]
This rewrites something like
http://www.website.com/thepagename.html
into
http://www.website.com/index.php?a=p&p=thepagename
which works fine.
Now I need to modify this to allow for an optional query parameter that may or nay not be tacked on to the original (unrewritten) URL. E.g.:
http://www.website.com/thepagename.html?req=login
or even
http://www.website.com/thepagename.html?req=login&usr=johndoe
must be rewritten into:
http://www.website.com/index.php?a=p&p=thepagename&req=login
and
http://www.website.com/index.php?a=p&p=thepagename&req=login&user=johndoe
respectively, without breaking the original rewrite (i.e. without the optional query parameters tacked onto the unrewritten URL).
Try as I might, I cannot work out the correct syntax. Can anyone point me in the right direction?
Tnx!
// FvW
You only have to add ˋQSA` flag (Query String Append)
RewriteRule ^([_0-9a-zA-Z-]+)\.html$ index.php?a=p&p=$1 [L,QSA,NC]
More info (and examples) here

Using mod_rewrite to put words in the URL instead of category IDs

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]