.htaccess URL redirect with parameters - wordpress

I have a link like this:
From: http://www.example.com/wp/?p=231
and I need to redirect it to this URL:
To: https://www.example.com/jump/
How could this be done?
This the .htaccess file:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

You need to use mod_rewrite to perform this specific redirect. And this rule should go at the very top of your .htaccess file, before all existing rules. For example:
RewriteEngine On
RewriteCond %{SERVER_PORT} =80
RewriteCond %{HTTP_HOST} =www.example.com
RewriteCond %{QUERY_STRING} ^p=231$
RewriteRule ^wp/$ https://%{HTTP_HOST}/jump/ [QSD,R=302,L]
UPDATE: Added the two additional conditions to check the SERVER_PORT and HTTP_HOST so that it only redirects http://www.example.com/wp/?p=231 exactly as stated. It then redirects directly to https://www.example.com/jump/.
The additional condition that checks against the QUERY_STRING server variable is necessary in order to match the query string, since the RewriteRule pattern matches against the URL-path only.
The QSD flag is necessary to remove the existing query string from the redirected response, otherwise this is passed through by default.

Depending on your .htaccess file you posted, the solution should be the following:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/wp/ [NC]
RewriteCond %{QUERY_STRING} p=231
RewriteRule (.*) /jump? [R=301]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
You can check it with the following link:
https://htaccess.madewithlove.com?share=d5a7976b-47f3-4143-bb7c-103a722f0b2d

Related

Rewrite domain to cut its URI

I need to set up the http://domain.com/wp1/ to be shown like http://domain.com/
I have tried to set up any rewrite in the past hours, but no success :(
P.S. There is a wordpress installed in the public_html/wp1, configured to work on http://domain.com/wp1/ URL. Its .htaccess is as follows:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wp1/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wp1/index.php [L]
</IfModule>
# END WordPress
Thanks in tons!
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain-name.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain-name.com$
RewriteCond %{REQUEST_URI} !folder/
RewriteRule (.*) /folder/$1 [L]
Changing the document root for the domain does the job.

Issue with .htaccess redirecting all pages to HTTPS except one

I have the code below in my .htaccess to redirect all pages to https except one (/wc-api/v2), which must NOT use SSL.
It does redirect all pages to https successfully, but if I go to /wc-api/v2, it redirects me to /index.php.
# Custom Redirect
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !^on$
RewriteCond %{REQUEST_URI} !^/wc-api/v2
RewriteRule (.*) https://example.com/$1 [R,L]
</IfModule>
# End Custom Redirects
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
FYI The second block of redirects is required for Wordpress. I also cannot combine them into one single block or Wordpress will overwrite my changes.
When rewriting is configured in .htaccess context, after your last rule is applied, the whole process starts over again, using the internally rewritten URL as the new “input”, until no more rules match.
That means, when you request /wc-api/v2, it is not rewritten to HTTPS, because it fails your first RewriteCond, and so the rule in the next block rewrites it to /index.php internally.
Now the “next round” starts, with /index.php as input. RewriteCond %{REQUEST_URI} !^/wc-api/v2 now does result in “true”, because the REQUEST_URI is not /wc-api/v2 any more, it is now /index.php. Therefor, the RewriteRule is applied – and you are redirected to https://example.com/index.php
To avoid this, you must add another RewriteCond, which will prevent the rule from being applied for the REQUEST_URI /index.php as well:
RewriteCond %{HTTPS} !^on$
RewriteCond %{REQUEST_URI} !^/wc-api/v2
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule (.*) https://example.com/$1 [R,L]
I got an error on my server with the suggestion above so modified it slightly and found that this worked for me:
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{REQUEST_URI} !^/wc-api/v2
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule (.*) https://www.example.com/$1 [R,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Hope it works for you.

Redirect all Wordpress pages to SSL apart from one

I want to redirect all pages on my Wordpress website to SSL apart from one.
I have tried numerous ways to do this and have succeessfully got the SSL working, but I cannot get the single page exclusion to work.
I am assuming the problem lies with the Wordpress specific rewrites.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{REQUEST_URI} ^parent-page/child-page/
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</IfModule>
# END WordPress
Any suggestions?
You always need your rules that redirect to be before the rules that internally rewrite. Otherwise, internal stuff (like /index.php) end up getting redirected.
Try:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{REQUEST_URI} !^/parent-page/child-page/
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{REQUEST_URI} ^/parent-page/child-page/
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Note that you also need to add a / leading slash to your %{REQUEST_URI} conditions, those will always start with a /. Additionally, you need to not redirect to https with that URI, so you need to add a negative exclusion condition to the redirect to https.

Wordpress messing up rewrite rules

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.app.domain.com$
RewriteCond %{REQUEST_URI} !^/app/
RewriteRule (.*) /app/$1
RewriteCond %{HTTP_HOST} ^app.domain.com$
RewriteCond %{REQUEST_URI} !^/app/
RewriteRule (.*) /app/$1
The problem: app.domain.com is working without problems, but as soon as you add a subfolder like app.domain.com/folder you get the Wordpress tag page.
What am I doing wrong?
The problem is here
RewriteCond %{REQUEST_URI} !^/app/
RewriteRule (.*) /app/$1
Which says that if the URL does not start with /app/ (which /folder doesn't) it must be rewritten to /app/folder. After that rewrite the RewriteRules will be processed again, and the wordpress section will find that folder doesn't exist and fire up wordpress for you.
The way to resolve this issue is to change the last block to
RewriteCond %{HTTP_HOST} ^(www\.)?app\.domain\.com$
RewriteRule %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/app/
RewriteRule .? app%{REQUEST_URI} [L]
This tells apache it may only rewrite to /app/folder if /folder does not exist.
(and you can remove the whole stuff for app.domain.com, this block handles both www.app.domain.com as well as app.domain.com

htaccess and no www

I am trying to redirect my whole site to non-www
here is the htaccess code I am using
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# no www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.akorra\.com$ [NC]
RewriteRule ^.*$ http://akorra.com%{REQUEST_URI} [R=301,L]
any ideas
I think your rules need to between the module tags.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# no www
RewriteCond %{HTTP_HOST} ^www\.akorra\.com$ [NC]
RewriteRule ^.*$ http://akorra.com%{REQUEST_URI} [R=301,L]
# BEGIN WordPress
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
</IfModule>
Here is what you need to add to your .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
However, if you are working with Wordpress as the engine that powers your entire site, just update the permalink structure and all internal settings with your domain name in it to remove the www.
EDIT:
I thought that was different when I wrote the answer Sorry. Try moving your non www rule to the top...
# no www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.akorra\.com$ [NC]
RewriteRule ^.*$ http://akorra.com%{REQUEST_URI} [R=301s,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
AGAIN keep in mind if Wordpress is set to produce links with a www then nothing will stop it from reverting everything back to having a www even with a correct rewrite rule.
More Wordpress details:
Check the settings in wp/wp-admin/options-general.php ...
Be sure to remove the www in WordPress address (URL)
Be sure to remove the www in Blog address (URL)
AND then update your premalink structure in wp-admin/options-permalink.php so the changes are reflected.
After all that is said and done, be sure Wordpress did not overwrite new code in your .htaccess file.
Hi I think you need to adjust your script as follows for the rewriting to work:
Options +FollowSymlinks
RewriteEngine on
I believe that the follow symlinks absolutely has to be included for url rewriting to work properly.
More advice on mod_rewrite here:
http://www.askapache.com/htaccess/mod_rewrite-tips-and-tricks.html
Rob
RewriteEngine On
RewriteBase /
# no www
RewriteCond %{HTTP_HOST} ^([^.]+)\.akorra\.com$ [NC]
RewriteRule ^(.*)$ http://akorra.com/$1 [R=301,L]
# WordPres
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
This should work
I suggest to use this piece of code for removing www from your website:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
it's more generic.
and in your example it's best to use this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# no www
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
# BEGIN WordPress
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
</IfModule>

Resources