Changing query string key name with htaccess rewrite rule - .htaccess

We are changing affiliate software and I can't figure out how to rewrite the name of the query string key to achieve the following:
https://www.example.com/path/to/productx?old_key_name=numeric_value
Redirect to:
https://www.example.com/path/to/productx?new_key_name=numeric_value
So far I have:
RewriteCond %{QUERY_STRING} ^old_key_name=([0-9]+)
RewriteRule ^ %{REQUEST_URI}?new_key_name=%1 [L,R=301]
Which doesn't retain the full path to the product, but results in:
https://www.example.com/index.php?new_key_name=numeric_value
/path/to/productx/ is missing.
I'm placing the rule at the bottom of a Joomla htaccess file on an Apache server. Can the full path to the product be added to the redirect rule?

I've just moved the rule to the top of the htaccess file and it works as expected. I guess the default Joomla SEF Rewrite rules were the problem.

Related

Rewrite Rule for Masking the URL with Query string and Page number

I am trying to mask the following url with any page number using htaccess.
https://sampledomain.com/?page=2
to
https://sampledomain.com/page/2
So if we call https://sampledomain.com/page/2 url in the website, internally path should always be calling https://sampledomain.com/?page=2 url
Please suggest me the correct htaccess rule.
In htaccess in your document root, use the following rule :
RewriteEngine on
RewriteRule ^page/([0-9]+)/?$ /?page=$1 [QSA,L]

Dynamik redirect with htaccess and path structure

I'd like to setup an htaccess redirect rule which redirects the following pattern:
https://www.example.com/something/ABC123455
to
https://www.new-domain.com/something/ABC123455
while ABC123455 is a dynamic string which can include any upper cased letter and number with exactly 8 characters.
So I guess the regex for this would be:
([A-Z0-9]){8}
So I am looking into several guides:
Redirect to dynamic relative paths with .htaccess?
https://www.sitepoint.com/community/t/htaccess-redirect-with-dynamic-strings/74591
but all I learn is how to setup a route redirect via query parameter ?var=ABC and so on.
Can someone bring me to the right track here? It can't be that hard.
You may use this rule in site root .htaccess of old site:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^something/[A-Z\d]{8}/?$ https://www.new-domain.com/$0 [L,R=301]

Amend .htaccess path rewrite to include subdirectory

I currently have the following .htaccess rewrite rule setup on my site:
RewriteEngine On
RewriteRule ^([^/d]+)/?$ index.html?id=$1 [QSA]
The rule works in such a way that if I go to the following URL:
http://example.com/dashboard
It won't try and find the dashboard directory that doesn't exist but instead it will keep the URL as is and redirect the user to the root index page. From there I just use javascript to control what view the user will see depending on what path is appended.
The code works exactly as I want it to but i've now had to move my site into a sub-directory on our server. The URL structure is now this:
http://example.com/mysubdir/dashboard
I tried rewriting my rewrite rule to incorporate the directory but have not been successful so far as i'm no .htaccess expert. I tried something along the likes of:
RewriteEngine On
RewriteRule ^([^/d]+)/mysubdir/?$ index.html?id=$1 [QSA]
Could anyone tell me how I can amend my rewrite rule to work in my sub-directory?
You were close - this should do it:
RewriteEngine On
RewriteRule ^mysubdir/([^/d]+)/?$ /mysubdir/index.html?id=$1 [QSA]
Demo here: http://htaccess.mwl.be?share=6e574cc6-90a4-54ca-b113-ce72d6eb5203

Remove directory from URL with

I am moving a Magento site from example.com/shopping to example.com and I have to 301 redirect all of the URLs to the new location (example.com). I am assuming I can use mod_rewrite in the .htaccess file to create a rewrite rule to do this? I tried to learn how to use mod_rewrite, but the syntax is so complex for me.
There are hundreds of pages that need to be redirected. Example:
http://www.example.com/shopping/product-category-1/product-sub-category.html
TO
http://www.example.com/product-category-1/product-sub-category.html
This is so that people don't end up with a 404 error when they enter the site via an old URL.
Try adding this to the htaccess file in your document root, preferably above any rules you may already have there:
RewriteEngine On
RewriteRule ^shopping/(.*)$ /$1 [L]
If you want to redirect the browser so that the new URL appears in the location bar, add an R flag in square brackets:
RewriteEngine On
RewriteRule ^shopping/(.*)$ /$1 [L,R=301]

htacess url rewrites for .php?=n

I'm looking to rewrite old PHP pages to new locations, however the previous system used URLs structured as follows:
/old-page.php?page=Our%20Services
I've tried the following:
Redirect 301 /old-page.php?page=Our%20Services http://www.newdomain.co.uk/our-services/
Can someone help explain to me why the rewrite rule ignores everything after the question mark please?
You can use Redirect for simple cases only. If you want to check the query string, you must use mod_rewrite and RewriteCond
RewriteEngine on
RewriteCond %{QUERY_STRING} page=Our%20Services
RewriteRule ^old-page.php$ http://www.newdomain.co.uk/our-services/ [R,L]
This checks, if the query string contains page=Our%20Services and the URL path is old-page.php. It then redirects the client to the new URL http://www.newdomain.co.uk/our-services/.

Resources