RewriteCond %{QUERY_STRING} for new site - php

I have a really old Joomla-1.0 site with tens of thousands of articles that we recently upgraded to a current version of Joomla with SEF. This is on apache-2.4.34 on fedora29.
The problem is that it doesn't support the old URL format any longer, so I'm trying to rewrite many of the old URL formats to work with the site.
The one I'm having the most difficulty with contains a query string:
example.com/index.php?option=login
This should redirect to example.com/register
My rewrite rules are not working, however:
# /index.php?option=login
RewriteEngine on
RewriteCond %{QUERY_STRING} ^option=login$
RewriteRule ^/index.php$ https://linuxsecurity.com/register [R=301,L,QSD]
This is placed in the .htaccess file in the document root. I've also tried placing it in a section within the virtual domain config.
It appears it's being ignored entirely. It reports an error like "Component not found", as if it's being interpreted by joomla.
I also have several other RewriteRule entries below that don't appear to work. They just report 404 pages. If I enter the article IDs into the replacement link, it works fine.
RewriteRule ^/content/view/([^/]+) /index.php?option=com_content&view=article&id=$1 [R=301,L]
I'd appreciate any advice you may be able to provide.

Your condition is good, but need to tweak the rule for the login one. Please try:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^option=login$
RewriteRule ^(.*) https://linuxsecurity.com/register [R=301,L,QSD]
For the other content redirects, this is what I have used for Joomla 1.0 to Joomla 3.x before:
RewriteRule ^content/view/(.*)$ /index.php?option=com_content&view=article&id=$1 [L,R=301]
https://www.yellowwebmonkey.com/developer-blog/item/redirect-joomla-1-0-sef-links-for-joomla-3-x

Related

htaccess redirect in wordpress

By using WordPress with Divi and custom php pages/scripts I ended with urls looking like this:
https://example.com/?day=today
https://example.com/?sport=1&league=123 (1=football and 123=premier_league)
I want to make them more user and SEO friendly and rewrite them in htaccess to:
https://example.com/?day=today ==> https://example.com/today
https://example.com/?sport=1&league=123 ==> https://example.com/football/premier_league
For the "day" link I tried this code, but it doesn't work
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-z\-\_]+)?$ /?day=$1 [L,QSA,NC]
</IfModule>
For the other example with sport and league I wanted to find a solution in which I will manually add old link and new link, tried redirect 301, but I think it can't work, right?
Redirect 301 https://example.com/?sport=1&league=123 https://example.com/football/premier_league
I also tried Redirection plugin in WP, but it created Rule which destroys page and makes error 500:
RewriteRule ?sport=1&liga=123 https://example.com/football/premier_league [R=301,L]
or
RewriteCond %{QUERY_STRING} ^sport=1&league=123$
RewriteRule ^$ https://example.com/football/premier_league [R=301,L]
which gaved address: https://example.com/premier_league/?sport=1&league=123
I was also wondering if I can make it with pure php, since in PHP I can easilly get proper strings for id's of the sports and leagues. Unfortunantelly changing "?sport=1&league=32758" into "?sport=football&league=premier_league" and then clean it with rewrite in htacces is not possible right now.
I'm not expecting whole code for both problems, I believe that some directions would be enough. I browse stack and google and coudn't find any examples with links that looks like mine, so example.com/?sport=1 not example.com/page?sport=1
After further investigation I believe the problem is in the way I'm opening my custom pages. I'm using shortcodes to initiate a script and inside the script my php code is included with parameters. I think that because of that I got to this page https://example.com/premier_league/?sport=1&league=123 which was pretty close, but not working.
Now my question is how to send those parameters to proper script but not into url.

Wordpress .htaccess causing redirect loop

I've been facing a problem with my WordPress app for over a month now and I couldn't make it work. I thought it was a problem with DNS server, then I thought I had a problem of caching, then a problem with the application (the wordpress redirecting it wrongly) and now I believe I have a problem with .htaccess
If you go to www.porta8080.com.br my website work fine, but when I remove the WWW it makes a redirect loop. Somehow it is not changing the URL, so it reloads the page and it's redirected again and again and again. I checked with cURL and same happens.
If I remove the .htaccess and change the permalinks settings to Query Strings it works. But anything that relies on URL rewriting (friendly URLs and stuff like that) fail.
Since I'm using OpenShift I think my conditions and rules are wrong. I'm using the default WP .htaccess and I added some things to force the WWW
This is my htaccess at the moment
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
First of all to enforce "www" in WordPress, there is simple, non-htaccess based solution.
i.e. Keep using standard htaccess file of WordPress and for www version. Go to WordPress Settings -> General
There you have two fields for your site's url: WordPress Address (URL) & Site Address (URL) , make sure both urls have http://www version in it.
Then WordPress will enforce www version for all Urls of your website.
This may solve your htaccess and redirect loop issue also?

.htaccess RewriteEngine converting links without changing links

I'm on my begining of learning PHP and very first steps of .htaccess, most of my new web is about main category and few subcategories.
Here are links examples i had before working out .htaccess RewriteEngine:
example.com/index.php?cat=email
example.com/index.php?cat=about&show=some
with help of .htaccess RewriteEngine i've convered them to:
example.com/email/
example.com/about/some/
Here is part of .htaccess:
RewriteRule ^([A-Za-z0-9-]+)/$ index.php?cat=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$ index.php?cat=$1&show=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)$ index.php?cat=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?cat=$1&show=$2 [L]
Now problem is that most of content have inside links like: "example.com/index.php?cat=about&show=some" Changing them all is option, but anyway... is there anything else could be done? I heard of some .htaccess option that autoconverts links to format you need without changing them manualy, so all links in PHP pages will be the same, but once user gets page loaded, links will be like (example.com/about/some/) Is there anything like that, or is there any other option to leave original link without changing them all?
Cheers!
Links on your site are created with your PHP scripts, Apache with htaccess can't magically change all this links in a raw.
Now what you can do, is redirect old URL to the new ones using htaccess, with a 301 redirection.
For example, if someone click on example.com/index.php?cat=email, Apache will redirect (= the URL will be changed and there will be another HTTP request) to example.com/email/, and then rewrite this to index.php?cat=email
Add this to your htaccess :
RewriteCond %{REQUEST_FILENAME} index.php$
RewriteCond %{QUERY_STRING} cat=([a-zA-Z0-9-]+)
RewriteRule ^ http://example.com/%1/? [L,R=301]
Anyway I strongly recommend you to change the links directly in your code, because even if the solution I've just explained should works (not tested), it's not really healthy to use redirection when you can avoid them.

.htaccess dynamic url (old site) to static url in magento

Long time reader first time poster.
I have read through what feels like 100's on similar posts trying to make it fit what i am trying to do.
Apologies if its been covered. I believe I have the correct code but it maybe conflicting with something else?
I have old URLs from an old ASP cart and want to 301 redirect to new magneto static pages.
There are only 5 of these URLS so don't need a bulk action.
example:
OLD URL: http://test.com.au/shopcontent.asp?type=FAQ%20and%20Terms
NEW URL: http://test.com.au/privacy-policy-cookie-restriction-mode
I know that from reading other posts you can not simply use:
Redirect 301 /shopcontent.asp?type=FAQ%20and%20Terms /privacy-policy-cookie-restriction-mode
So I have worked out that this should work:
RewriteCond %{QUERY_STRING} ^type=FAQ%20and%20Terms$ [NC]
RewriteRule ^shopcontent\.asp$ /privacy-policy-cookie-restriction-mode/? [NC,R=301,L]
I have also tried removing the %20 for the spaces like this:
RewriteCond %{QUERY_STRING} ^type=FAQ\ and\ Terms$ [NC]
RewriteRule ^shopcontent\.asp$ /privacy-policy-cookie-restriction-mode/? [NC,R=301,L]
When I try and access the old URL it just goes to the 404 page. I am using standard .htaccess file that came with Magento (I can post if that helps);
I am using a VPS which mod rewrite is enabled. I have other Redirect 301's for non dynamic URLS.
Are there any logs that I can look at?
Appreciate any help or advice.
Thanks Rudi
ps. I found this post which got me to where i am now
RewriteCond %{QUERY_STRING} in mod_rewrite (dynamic to static URL) not working
You cant use this rule:
RewriteEngine On
RewriteCond %{QUERY_STRING} "^type=FAQ\%20and\%20Terms$" [NC]
RewriteRule ^shopcontent\.asp$ /privacy-policy-cookie-restriction-mode/? [NC,R=301,L]

Mod_Rewrite link to external url without parameters

i changed my cms and have now some old urls, that are still linked by other websites.
Now i have to redirect the old links to my new sites Start Page.
The old CMS was installed in a subfolder named "contentms". The new CMS is installed in the root-folder.
Old Urls look like: mywebsite.com/contentms/content.php?idcat=62
When i try to forward it using my rewrite rule:
RewriteEngine on
RewriteRule ^contentms/.*$ http://www.mywebsite.com/ [R=301,L]
It works, but there will appear:
http://www.mywebsite.com/?idcat=62
But i want to have only http://www.mywebsite.com, without the "?idcat=62" added to the external url.
Maybe you have a solution for me.
Thanks!
Give this a try and see how it works for you.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/contentms
RewriteRule ^(.*) http://www.mywebsite.com/? [R=301,L]
You can do this in the same line.
RewriteEngine on
RewriteRule ^contentms/.*$ http://www.mywebsite.com/? [R=301,L]

Categories