How to rewrite url remove slug /product/ - wordpress

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.

Related

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]

.htaccess force trailing slash but exclude images

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

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]

Using additional Re-Write Rules in WordPress .htaccess to Stop DDOS attacks

We have suddenly been the victim of what we are being told is a DDOS attack. We are trying to use some .htaccess rules to block the affending parameters, using the following, to show a 403 forbidden page to any posts requests made on the home page and any requests with specific fields in the beginning of the query string:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(ptrxcz|xclzve).* [NC]
RewriteRule ^(.*)$ http://%{REMOTE_ADDR} [F,L]
RewriteEngine On
RewriteCond %{REQUEST_METHOD} POST
RewriteCond %{REQUEST_URI} /
RewriteRule ^(.*)$ http://%{REMOTE_ADDR} [F,L]
Basically this works fine whenever I use wget to access the file with the offending query string in SSH but not in the browser window.
In addition, the Postmaster Google Chrome add on blocks the post requests to the homepage however a curl -d request doesn't get blocked so I'm finding it hard on which tools to trust as they are giving varying results (browser, SSH, Postmaster).
I am also using Wordpress with permalinks and I wonder if these affect this in some way as these are the only other things in the .htaccess file. Once I remove these from the .htaccess file I then everything works as expected. This is the Wordpress .htaccess which comes after my current rules.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
In addition, if I change my pre-Wordpress rules and remove the [F] and REMOTE_ADDR parts and replace it with the following to redirect it to a URL that doesn't exist this redirects properly, which means it could be related to the [F] and REMOTE_ADDR sections.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(ptrxcz|xclzve).* [NC]
RewriteRule ^(.*)$ http://www.pleasegoawayandnevercomebackagain.com [R=301,L]
RewriteEngine On
RewriteCond %{REQUEST_METHOD} POST
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://www.pleasegoawayandnevercomebackagain.com [R=301,L]
In addition to the above and on further investigation. The following rule DOES work:
RewriteEngine On
RewriteCond %{QUERY_STRING} !.*test.* [NC]
RewriteRule ^(.*)$ http://%{REMOTE_ADDR} [F]
The difference here is I'm checking for a negative match on the query string value. This means every page is forbidden except for those with test in the query string. Do you have any idea why a negative match works but a positive search does not? Is it a syntax issue or something to do with searching positive values in htaccess Rewrite Conditions?
Some ideas that may help you:
Make sure you maintain # BEGIN WordPress and # END WordPress in your .htaccess file. place the rest on your rules above it
try installing the plugin: Better WP Security: http://wordpress.org/extend/plugins/better-wp-security/ - it's a life saver :)
If you replace QUERY_STRING in the code above with THE_REQUEST then this works fine and does exactly as required within WordPress.

Resources