How to redirect https://www.themaidsofcharleston.com/?Itemid= to https://www.themaidsofcharleston.com/
I have tried
RewriteCond %{QUERY_STRING} ^?Itemid=$
RewriteRule ^/$ /? [R=301,L]
but id doesn't work
Thank you in advance.
Two issues needed fixing here:
{QUERY_STRING} does not contain the leading question mark - you want to check for ^Itemid=$
when configuring rewriting in .htaccess context, the path never starts with a /, that has been stripped off at that point already. So your rule needs to match an empty path instead, ^$
Related
I need to redirect users with old links to a subdomain to a new link that is dynamically generated using information from the link.
For example:
Old Link will look like this:
subdomain-2013.old-domain.com/data/process/aqs-2d3f4f5g-fgtgyy-uunb.xml
To construct the new link, I need the year provided in the subdomain and the file name. (The year will change, it is not constant).
New Link:
new-domain.com/2013/data/process/aqs-2d3f4f5g-fgtgyy-uunb.xml
How is this done? Is this something accomplished with .htaccess?
You can do it like this using mod_rewrite near the top of the root .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain-(\d{4})\.(example\.com) [NC]
RewriteRule ^data/process/([a-z0-9-]+\.xml)$ https://%2/%1/$1 [R=301,L]
Where %1 and %2 are backreferences to the preceding CondPattern and $1 is a backreference to the captured group in the RewriteRule pattern.
HOWEVER, I've just noticed you've tagged the question wpengine. The webhost "WP Engine" no longer supports .htaccess config files. Reference: https://wpengine.com/support/htaccess-deprecation/ - UPDATE: Although it seems this tag has since been removed from the question.
UPDATE: If the target domain is different (a later change to the question) then this would need to be hardcoded in the substitution string, replacing the %2 backreference. For example:
RewriteCond %{HTTP_HOST} ^subdomain-(\d{4})\.old-domain\.com [NC]
RewriteRule ^data/process/([a-z0-9-]+\.xml)$ https://new-domain.com/%1/$1 [R=301,L]
The example in the question is passing just the .xml filename to the target URL, it is not passing the preceding URL-path, which is what the rule above is doing.
I found two sites that were instrumental in solving this problem and then testing the solution, linked below.
Here is the solution:
RewriteCond %{HTTP_HOST} ^.*(\d{4})\.(.*?)$
RewriteRule . http://www.new-domain.com/%1%{REQUEST_URI} [R=301,L]
https://regex101.com/r/XFMOwW/1
https://htaccess.madewithlove.com?share=3ea90fee-86df-488e-9346-b0797c26a3f1
I have searched but cannot find a specific answer for this exact redirect style...
I have this structure of URL with this specific parameter:
https://websitename.com/directory/index.php?option=com_virtuemart&view=cart
I want it redirected to:
https://websitename.com/shopping-cart/
Note that the above mentioned "directory" changes, but the index.php with the parameters stay the same. No matter what the directory is, I always want it to go to the same exact redirect.
I cannot seem to get the right redirect working in htaccess. Can anyone help?
You can use this redirect rule as your first rule in site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} /(?:index\.php)?\?option=com_virtuemart&view=cart [NC]
RewriteRule ^ /shopping-cart/? [L,R=308]
# remaining rules go below this
You can use a set like this. It takes care on the param view=cart
RewriteCond %{QUERY_STRING} (^|&)view=cart
RewriteRule ^(.*)$ /shopping-cart/? [L,NC,R=301]
If you want to keep the querystring params, then change
/shopping-cart/?
to
/shopping-cart/
without questionmark
I have been unable to get a redirect to work. The behavior I was hoping to have would redirect from:
www.mysite.com/?id=12345
to
www.mysite.com/?other_id=12345
The rule I wrote seems to grab any url matching the very end like:
www.mysite.com/directory/another/?id=12345
I just want this to work at the root level, not all levels.
The rule I currently have is as follows:
RewriteCond %{QUERY_STRING} ^(.*&)?id=([^&]+)$
RewriteRule ^(.*)$ http://%{SERVER_NAME}/?other_id=%2 [R=301,L]
For some reason I had to include the first regex as it was not accepting it without it. Without the first ^(.*&) It kept saying that the site was creating a redirect that would not work and bombing out, which may be a part of my problem.
IS there a better way to write this rule? or is a root level match alone not possible?
Thank you,
Silvertiger
Change your regex from ^(.*)$ to `^$``:
RewriteCond %{QUERY_STRING} ^(.*&)?id=([^&]+)$
RewriteRule ^$ http://%{SERVER_NAME}/?other_id=%2 [R=301,L]
The ^(.*)$ regex matches any request URI, so /, and /foo and /dir1/dir2/dir3/file.txt will all match. But the ^$ only matches /.
I have a site with a folder, and a htaccess file within that folder. For the index.php file within that folder, I want to rewrite the querystring for a certain parameter, so that typing in this URL:
www.example.com/myfolder/myparameter
Behaves like this (ie makes $_GET['parameter'] = 'myparameter' in my code)
www.example.com/myfolder/index.php?parameter=myparameter
I have looked at many questions on StackOverflow, but have not managed to get this working. My code so far is
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*) [NC]
RewriteRule ^$ %0 [QSA]
But that just isn't working at all.
Please use this code
RewriteEngine on
RewriteRule (.*) index\.php?parameter=$1 [L,QSA]
RewriteEngine on
RewriteRule (^.*/)([^/]+)$ $1index\.php?parameter=$2 [L,QSA]
update
sorry use #somasundaram's answer. Per-directory .htaccess rewrite rules lose the directory prefix:
When using the rewrite engine in .htaccess files the per-directory prefix (which always is the same for a specific directory) is automatically removed for the RewriteRule pattern matching and automatically added after any relative (not starting with a slash or protocol name) substitution encounters the end of a rule set. See the RewriteBase directive for more information regarding what prefix will be added back to relative substitutions.
(from the apache docs)
I'm trying to come up with some mod_rewrite to translate http://example.com/?7gudznrxdnu into http://example.com/view.php?id=7gudznrxdnu
But any other page will function properly such as http://example.com/contact and so on.
I think this will work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^[a-z0-9]+$
RewriteRule ^$ view.php?id=%{QUERY_STRING} [L]
If you want the rewrite to be shown in the browser's address field, you'll have to replace [L] with [L,R=301].
Explanation: The query-string (what's following the question mark) is not part of the URL that RewriteRule sees in its matching-pattern, therefore you can't check for question mark there. In my solution, I run the rule if and only if (RewriteCond) the query string consists solely of a-z and/or 0-9, and my rule only rewrites URLs ending with a slash (except for the query string). I redirect this to view.php?id=, and then append the query string to that.
Edit: Tested on my Apache-server, and I haven't found any bugs (yet).
You should try (in your .htaccess):
RewriteEngine On
RewriteRule ^\?([^/\.]+)?$ view.php?id=$1 [L]