Why isn't my htaccess RewriteRule working for my query strings? - apache

I am moving an ASPX site to Wordpres -- from an IIS server to Apache -- and I am having problems getting the pages with query strings to redirect. I looked into the Apache documentation and am trying to use mod_rewrite, but so far no luck.
For example I would like to redirect these pages:
www.mysite/Product/Product.aspx?mapid=324$ to
www.mysite/blue-widgets/widget7/
www.mysite/Product/Product.aspx?mapid=681$ to
www.mysite/blue-widgets/widget12/
www.mysite/Product/Product.aspx?mapid=841$ to
www.mysite/blue-widgets/widget82/
Here is where I am at. I have tried several iterations of this but can't get it to work. I am starting to think that it might have something to do with first few Rules, which were generated by the server and Wordpress. The site is an addon domain, which the server treats like a subdomain.
My rewrites are the last three. Any ideas why these aren't working? Thanks for any help.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~username/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /~username/mysite/index.php [L]
RewriteCond %{QUERY_STRING} ^mapid=324$ [NC]
RewriteRule ^/Product/Product\.aspx(.*)$ ^https://www.mysite/blue-widgets/widget7/? [L,R=302,NC]
RewriteCond %{QUERY_STRING} ^mapid=681$ [NC]
RewriteRule ^/Product/Product\.aspx(.*)$ ^https://www.mysite/blue-widgets/widget12/? [L,R=302,NC]
RewriteCond %{QUERY_STRING} ^mapid=841$ [NC]
RewriteRule ^/Product/Product\.aspx(.*)$ ^https://www.mysite/blue-widgets/widget82/? [L,R=302,NC]
</IfModule>

I believe the following will work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^mapid=324$ [NC]
RewriteRule /Product/Product\.aspx https://www.example.com/blue-widgets/widget7/? [L,R=302,NC]
RewriteCond %{QUERY_STRING} ^mapid=681$ [NC]
RewriteRule /Product/Product\.aspx https://www.example/blue-widgets/widget12/? [L,R=302,NC]
RewriteCond %{QUERY_STRING} ^mapid=841$ [NC]
RewriteRule /Product/Product\.aspx https://www.example/blue-widgets/widget82/? [L,R=302,NC]
RewriteBase /~username/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /~username/mysite/index.php [L]
To better explain for others, the order of the rules is important. Since the Wordpress rules change the RewriteBase and change the URL the rules for the .aspx pages will never be hit if they are at the bottom.

Related

redirect loop with mod_rewrite

I'm trying to add a language prefix to my pages through mod_rewrite
eg:
https://example.com/ > https://example.com/en/
and internally it need to translate to `https://example.com/?language=en
I've managed to do it for the base url, but when I try to do it with some other redirect implied, it's always end in an infinite loop.
eg:
https://example.com/product-p-22.html > https://example.com/en/product-p-22.html
and internally became `https://example.com/product_info.php?product_id=22&language=en
here is my actual config:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/(en|fr)/
RewriteCond %{REQUEST_URI} .(php|html) [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} !language=
RewriteRule ^(.*)$ en/$1 [L,R=301]
RewriteCond %{REQUEST_URI} !^/example
RewriteRule ^(.*)/$ index.php?language=$1&%{QUERY_STRING}
RewriteRule ^(.*)/(.*)-p-(.*).html$ product_info.php?products_id=$3&language=$1&%{QUERY_STRING}
I've tried many flags like L, END, DPI or some combination of those, but with no luck.
when I look at the debug logs, it seems to find the right url, but then restart parsing the default url:
pass through /var/www/example/product_info.php
init rewrite engine with requested uri /product-p-22.html
pass through /product-p-22.html
any though on what I'm doing wrong here?
Server version: Apache/2.4.41 (Ubuntu)
Based on your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^en [NC]
RewriteRule ^(.*)$ en/$1 [NC,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php?language=en [NC,QSA,L]
RewriteRule ^([^/]*)/([^-]*)-p-([^.]*)\.html$ product_info.php?products_id=$3&language=$1& [NC,L,QSA]
Have it this way in your site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} !\s/+(en|fr)/ [NC]
RewriteRule ^ /en%{REQUEST_URI} [NC,R=301,L]
RewriteRule ^([a-z]{2})/[^-/]+-p-([^./]+)\.html?$ product_info.php?language=$1&products_id=$2 [L,QSA]
RewriteRule ^([a-z]{2})/$ index.php?language=$1 [QSA,L]

httaccess change mydomain.com/knowledgebase.php to mydomain.com

I checked many other similar questions but I can't come up with a .htaccess rule that would work properly.
I have a server containing a Knowledgebase system (knowledgebase.php). I don't want to show this knowledgebase.php in the URL, ever.
Examples what I want:
https://help.thecrypto.app should show same url (now it shows https://help.thecrypto.app/knowledgebase.php when you visit)
https://help.thecrypto.app/knowledgebase.php?article=1 should show URL https://help.thecrypto.app/?article=1
How can I do this?
I tried many options, including this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /knowledgebase.php?/$1 [L]
Could you please try following, written based on your shown samples. Please clear your browser cache before testing urls.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/?$
RewritewriteRule ^ knowledgebase.php [L]
RewriteCond %{REQUEST_URI} ^/(knowledgebase) [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/?article=1 [L]
You can use this :
RewriteEngine on
#1) redirect "help.thecrypto.app/" to /knowledgebase.php
RewriteCond %{HTTP_HOST} ^help.thecrypto.app$ [NC]
RewriteRule ^knowledgebase\.php$ / [L,R]
#2) internally map knowledgebase.php to the root /
RewriteRule ^/?$ /knowledgebase.php [END]
This will serve /knowledgebase.php if you visit your site hompage / .

mod_rewrite rule for "www to non www" doesn't redirect sub-pages

I have made a thorough search before I have asked this question here. Please hear me out:
I am trying to redirect my blog from www to non www and it doesn't redirect any sub-pages. I have an http > https redirect in place as well and it works perfectly for both domain as well as the sub-pages. Here are the rules I have in my .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^^rcp-pep-ipn //?rcp-pep-listener=IPN [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
</IfModule>
# END WordPress
I'd really appreciate an explanation if I am doing anything wrong here. I have literally pulled my hair since I have used the exact same code (from the second RewriteBase /) for all other sites and it worked flawlessly.
You should bring those protocol checking conditions to the beginning. You have some problems within the rules too. Try:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,NE,L]
RewriteRule ^rcp-pep-ipn /?rcp-pep-listener=IPN [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

.htaccess Adding HTTPS forcing to existing rewrite rules of subdirectory

Probably I'm overseeing something very basic, htaccess is not my field of expertise.
My site is https-only, to achieve this, the root folder / contains these lines:
RewriteEngine On
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [R=301,L]
This works just fine. Now we have a new folder called /projects, which contains its own htaccess rules to translate the url in a database query by a file called /projects/project.html
The working htaccess within the folder /projects is:
RewriteEngine On
RewriteBase /projects/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ project.html?project=$1 [QSA,L]
Now I would like to force https connection on this one, because the https rule in the main directory is not applied on /projects if you visit by http://URL directly. Therefore I modified the file to:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://example.com%{REQUEST_URI} [R=301,L,NE]
RewriteBase /projects/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ project.html?project=$1 [QSA,L]
With this, it switches to https, but I keep getting "ERR_TOO_MANY_REDIRECTS".
When I change the lines as #thickguru suggested, like this:
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
It will redirect me to https://example.com/index.html right away.
I fiddled around with the settings, also with the information of this post, however still I can't get it to work.
I just noticed it the variable you are matching against is %{ENV:HTTPS} , so , you can use the following rule in /projects/.htaccess :
RewriteEngine on
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]
Clear your browser cache or try a diffrent browser to test this.
Use this in your .htaccess:
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
Also, place this above your your other RewriteRule, so that your .htaccess will look like:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
RewriteBase /projecten/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ project.html?project=$1 [QSA,L]
EDIT - Try this:
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

htaccess rewrite

I am working on a project and it utilizes a shortened url for redirection, I've gotten the following code to work perfectly:
XBitHack Off
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{REQUEST_URI} \/([0-9A-Za-z]{4})$ [NC]
RewriteRule ^(.*) ./getdeck.php?deck=%1 [L]
Now I want to add a second redirect rule for /a/([0-9A-Za-z]{4} but no matter how I write it, the new rule fails. Can anyone help explain what I'm missing?
Examples I have tried:
RewriteCond %{REQUEST_URI} ^/a/.*$ [NC]
RewriteRule ^(.*) ./deck/makedeck.php?deck=%1 [L]
or
RewriteCond %{REQUEST_URI} ^/a/([0-9A-Za-z]{4})$ [NC]
RewriteRule ^(.*) ./deck/makedeck.php?deck=%1 [L]
You might have to repeat the first two conditions.