.htaccess force trailing slash but exclude images - wordpress

I am using Wordpress and I am having an issue with including the trailing / on my urls. I have used RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301] however its adding an extra slash onto my images and making them not found.
How can I fix this?

The easiest way is definitely to remove all url containing a dot
RewriteRule ^([^.]+[^/])$ $1/ [L,R=301]

If you wanna exclude image from this mentioned rule , your code will be like this :
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png)$ [NC]
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=302]
So , by this condition RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png)$ [NC] you will be able to override any image with gif,jpe,jpeg or png extension from the rule of the last line

Related

How to rewrite url remove slug /product/

I have a sample link:
https://stackoverflow.com/test/index.php/product/01-00030am-1a1y
I want remove /product it become to
https://stackoverflow.com/test/index.php/01-00030am-1a1y
This is .htaccess
RewriteCond %{HTTP_HOST}
RewriteRule ^test/index.php/product/(.*)$ test/index.php/$2 [R=301]
But it never work, how to fix it
I believe its $1 not $2
$2 would refer to the second matched group which you haven't set it. Try:
RewriteCond %{HTTP_HOST}
RewriteRule ^test/index.php/product/(.*)$ /test/index.php/$1 [R=301,L]
The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed. Docs
Check here or here for more info about .htaccess rules.
Also please make sure that you have mod_rewrite on otherwise, it will not work. If not, I guess you can add it in your .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST}
RewriteRule ^test/index.php/product/(.*)$ /test/index.php/$1 [R=301,L]
As an alternative, If you're using WooCommerce, give woo-permalink-manager plugin a try.

Permanent redirect specific url query string

I searched on the site, but i didn´t find an exact answer.
I have multiple urls in a wordpress site with different parameters like this:
http://www.example.com/folder/?filter_color=353&orderby=price-desc
http://www.example.com/folder/?filter_material=345&orderby=date
http://www.example.com/folder/?filter_size=43&price-asc
I need to permanent redirect all of them to http://www.example.com/folder/
Is it possible with htaccess? Thanks!
EDIT 1:
I tried with this code but it doesn´t work. I want to redirect those old urls because they lead to a nothing found page. So after i copy this code and save the htaccess, nothing changes. The nothing found page still appear and the old url is shown in the browser. So it does nothing.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/folder$
RewriteCond %{QUERY_STRING} ^filter_material=([0-9]*)$
RewriteRule ^(.*)$ http://www.example.com/folder/ [R=301,L]
EDIT 2:
I finally achieved what i wanted with this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^filter_([\w]*) [NC]
RewriteRule ^folder/$ http://www.example.com/folder/? [R=301,NE,NC,L]
The rewrite condition for the query string is a regular expression, which is a tool for matching against search patterns.
The regular expression you are using – ^filter_material=([0-9]*)$ would match against URLs with a query string that starts with filter_material=, followed by an arbitrary number of digits.
The URL you want to redirect, however, does not match here, because it also contains &orderby=date, which is not all digits.
Since all the URLs you want to redirect start with filter_, you can simply use a regular expression that matches any string that starts with that, like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/folder/?$
RewriteCond %{QUERY_STRING} ^filter_.+$
RewriteRule ^(.*)$ http://www.example.com/folder/ [R=301,L]
The period means “any character”, the plus means “none, one, or more than one”.
Note that I've also added /? to the rewrite condition for the request URI, which means “there could or could not be a slash after folder”.
You can test this rewrite rule here: http://htaccess.mwl.be?share=2329d6f6-4fd5-5ff2-9035-a1957a242a42
I finally achieve what i wanted with this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^filter_([\w]*) [NC]
RewriteRule ^folder/$ http://www.example.com/folder/? [R=301,NE,NC,L]

Can't keep query string on URL

I run an Apache server with ISPConfig 3 installed and WordPress. I applied rewrite rules so that all HTTP goes to HTTPS and all WWW goes to non-WWW.
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [QSA,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com$1 [QSA,L]
I put this in my WordPress htaccess, and the example.com.vhost file created by ISPConfig 3.
The problem is now, even with the QSA in the flags, the query string is removed. I have tried the flags:
[L,R]
[R=301,L]
[R=301,L,QSA]
[L,QSA]
[QSA,L]
I have checked apache2.conf, 000-default from ISPConfig, example.com.vhost, and .htaccess files for any rules that do not have QSA or R and have not found any.
I'm a RewriteRule novice, but I'm trying to wrap my head around this, what am I doing wrong?
EDIT: While in my WP dashboard, I noticed that query strings are present in the URLs. So while on the frontend my server drops the query string, on the backend the query string is preserved. So I'm thinking that the WordPress's .htaccess must be the issue, right?
Try with:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
Test for www before test with https
No need to add [QSA] if you do not add ? and new query string.
While experimenting never use the R=301 permanent redirect because it will make live very hard for you. With Firefox you need to delete the entire cache just to try again. A useful trick can be using the private navigation mode to test. Then at least you can ditch it and start up another one.
Query strings would be transferred automatically in a [R] redirect. Unless maybe there is some QSD before or you put a ? inside the RewriteRule.
Can you try it like this?
RewriteEngine on
RewriteCond %{HTTPS} ^off$
RewriteRule . https://example.com%{REQUEST_URI} [R,L]
RewriteEngine on
RewriteCond %{HTTPS} ^on$
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule . https://example.com%{REQUEST_URI} [R,L]

htaccess and WordPress problems

I wanna redirect all my wordpress pages from one domain to another, such as:
from: http://domain1.com/page/
to: domain2.com/page/
So I added to htaccess of domain1.com:
RewriteRule (.*)$ http://www.domain2.com\/$1 [R=301,L]
But I want to make certain files exceptions to the above rule, so I also added:
RewriteCond %{REQUEST_URI} ^(images|javascripts|stylesheets|css|images-globa|js-global|js|htm|html).*
but the second rule doesn't seem to work. :(
Those aren't exceptions, those are "the request MUST be images/javascripts/stylesheets/etc in order to redirect", so you've got it backwards. You want a ! in there:
RewriteCond %{REQUEST_URI} !(images|javascripts|stylesheets|css|images-globa|js-global|js|htm|html)
RewriteRule (.*)$ http://www.domain2.com\/$1 [R=301,L]
Though I'm assuming you're way overreaching with the regular expression there, some of those look like you are matching against only the extension, so:
RewriteCond %{REQUEST_URI} !(images|javascripts|stylesheets|css|images-globa|js-global)
RewriteCond %{REQUEST_URI} !\.(js|html?|png|je?pg|gif)$ [NC]
RewriteRule (.*)$ http://www.domain2.com\/$1 [R=301,L]

How to configure .htaccess file for rewrite rules to get rid of extensions, trailing slashes, www?

This is what I want -
When somebody enters site.com/page.php or site.com/page or site.com/page/ , all should work the same, and the url displayed in the address bar should be site.com/page [ I don't like extensions and trailing slashes ]
I also want to get rid of the www from the url bar, as it's unnecessary.
This is my current .htaccess files Rewrite rules -
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site\.com
RewriteRule ^(.*)$ http://www.site.com/$1 [R=permanent,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This solves the www and the extension issue, but not the trailing slash one!
But, more importantly, I recently installed WordPress at site.com/blog, but whenever I try to access that, I am redirected to the 404 page instead. It works when I remove all the Rewrites!
Have I messed up my rewrites? I am not a regular expressions ninja!
Try adding the following to the top of your existing .htaccess file, above any existing rules e.g. Wordpress rules.
RewriteEngine on
RewriteBase /
#if the host is not site.com, e.g www.site.com
RewriteCond %{HTTP_HOST} !^site\.com$ [NC]
#then redirect to site.com e.g to remove the www
RewriteRule ^(.*)$ http://site.com/$1 [R=301,L]
#if the URL ends with a slash
RewriteCond %{REQUEST_URI} ^/(.+)/$
#redirect to URL without slash
RewriteRule . http://site.com/%1 [R=301,L]
#skip wordpress site which starts with blog
RewriteCond %{REQUEST_URI} !^/blog [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Resources