Single URL mod_rewrite keeps going back to the original URL - wordpress

I am trying to do a very simple single URL rewrite.
This is it:
RewriteRule ^blog /?post_type=post [NC,L]
I would simply like example.com/?post_type=pos to redirect and display example.com/blog.
I have tried a number of different versions of the rewrite but all I have achieved thus far is that I don't get a 404 on example.com/blog but keeps going back to example.com/?post_type=post.
I have placed RewriteRule right at the top of the .htaccess file which didn't help.
These are the other rewrites I have in the same .htaccess file:
#Single URL
RewriteRule ^blog ?post_type=post [NC,L]
#http www rewrite
RewriteCond %{HTTP_HOST} ^instrumentrentalbarcelona.com [NC]
RewriteRule ^(.*)$ https://www.instrumentrentalbarcelona.com/$1 [L,R=301,NC]
# 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

Thank you #DocRoot for helping me with this. The answer is detailed in the comment thread below by DocRoot and I. Just thought I would summarise this.
I wanted to show a page with all my blog posts.
This can be done if you use the WordPress post type "Post". example.com/?post_type=post
I wanted that page to show when you go to example.com/blog.
The following Rewrite works to show the desired URL: (thank you again #DocRoot)
RewriteCond %{QUERY_STRING} ^post_type=post$
RewriteRule ^$ /blog [QSD,R,L]
But /blog was giving a 404. So;
Create a page in WordPress called "blog"
Go to: Setting > Reading > Your homepage displays > Posts page: [Select page "blog"]
Save and done.

RewriteRule ^blog /?post_type=post [NC,L]
I would simply like example.com/?post_type=pos to redirect and display example.com/blog
The directive you posted does the exact opposite. It "rewrites" (not "redirects") from /blog<anything> to /?post_type=post
To redirect based on the query string you need an additional condition that checks against the QUERY_STRING server variable because the URL that is matched by the RewriteRule pattern is the URL-path only.
Try the following instead:
RewriteCond %{QUERY_STRING} ^post_type=post$
RewriteRule ^$ /blog [QSD,R,L]
This "redirects" from example.com/?post_type=post (exactly) to example.com/blog.
The QSD flag (Apache 2.4+) is required to remove the query string from the redirected request. If you are still on Apache 2.2 then you will need to append the substition with ? instead of using the QSD flag. eg. RewriteRule ^$ /blog? [R,L].
Not also, this is a temporary (302) redirect. If this should be permanent, then change the R flag to read R=301 - but only once you have confirmed this is working OK.

Related

.htaccess redirect "site/download/post/" to "site/post/"

Need some help here...
How can i permanently redirect with .htaccess this URL:
mysite.tdl/download/post-name/
to this URL:
mysite.tdl/post-name/#ert_pane1-5
Actually i would like to know how to redirect to this URL too:
mysite.tdl/post-name/
Its WordPress and Download Manager plugin related.
What im trying to do is avoid visitors to reach the package download pages, and be redirected to the actual post pages.
In other words, what i need is to eliminate the /download/ from all URLs.
Thank you.
My actual .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^(.*)/download/(.*) $1/$2 [R=301,L]
</IfModule>
The last line is the one in question. It possibly makes no sense because i was playing on it..
To eliminate the leading /download/
RewriteEngine On
RewriteRule ^download/(.*)$ /$1 [R=301,NC,L]
You can redirect by adding the following to your .htaccess
Redirect 301 /download/post-name/ /post-name/
Redirecting to an anchor (#) is not possible as this is a client side action. You could use Javascript for this.

Redirect url of wordpress site on the basis of string using htaccess

I have a wordpress site eg. http://www.domain.com/ with custom theme hosted on a linux server and working fine. Some marketing strategy requires me to distinguish between 2 kinds of URLs (that they will be shooting from ads) and based on that redirect the site to different links.
eg:
If the URL contains /xyz/ ie:
http://www.domain.com/xyz/category/post I want it to be redirected to
an intermediate page ie: http://www.domain.com/intermediate.php and if
the url doesnt contain /xyz/ ie:
http://www.domain.com/category/post the post should show up as
usual.
I found out that this cant be done inside the wordpress code as before the first hook is triggered, the url is processed and a 404 page is thrown.
The only way I can achieve this is by modifying .htaccess file.
I found a code which reads:
RewriteRule ^(.*)/xyz/(.*)$ http://www.domain.com/intermediate.php [L,R=301]
and second way reads:
RewriteCond %{REQUEST_URI} /xyz/
RewriteRule .* http://www.domain.com/intermediate.php
I am really confused about using it with the existing code in the htaccess file created by wordpress which reads:
# 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
I tried merging the codes ie:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteBase /domain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /domain/index.php [L]
RewriteCond %{REQUEST_URI} /xyz/
RewriteRule RewriteRule ^(.*)/xyz/(.*)$ http://domain.com/intermediate.php
</IfModule>
but only one of them work at a time ie: when the redirection works, wordpress posts doesnt show up and vice versa.
Kindly show me a better way.
Immediately after the RewriteEngine On and RewriteBase directives in the "existing" WordPress code try this:
RewriteRule xyz/ http://www.domain.com/intermediate.php [L,R=301]
This will search for "xyz/" anywhere in the requested URL and redirect when found. Note that in .htaccess files, the directory prefix (/ in this case) is removed from the URL path before pattern matching. So, a pattern that starts / will never match at the start of the URL.
External redirects should generally come before internal rewrites, which is what the default WordPress directives do.
The alternative method you mention is less efficient:
RewriteCond %{REQUEST_URI} /xyz/
RewriteRule .* http://www.domain.com/intermediate.php [R=301,L]
This will effectively do the same thing but will result in every request being processed because of the generic .* pattern. Note that the REQUEST_URI does start with a / (directory prefix).

.htaccess RewriteRule for WordPress: map example.com/yyyy/iframe/ to example.com/yyyy?iframe=1

In a WordPress website, all URLs with a trailing /iframe/ should be rewritten to ?iframe=1, e.g.:
mysite.com/page1/iframe/ should be rewritten as mysite.com/page1?iframe=1
For fun, I tried the following:
RewriteRule ^(.*)/iframe/$ http://microsoft.com [L]
Which is doing what I expected. The RegEx should be correct.
Next, I tried this:
RewriteRule ^(.*)/iframe/$ /$1?iframe=1
In context of WordPress' own rules, the complete file looks like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Here goes my line:
RewriteRule ^(.*)/iframe/$ /$1?iframe=1
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
But, despite all my tries and variations, the only thing I constantly get, is a 404.
Where am I going wrong?
When you do the redirect to ?iframe=1, you didn't add the [R] flag, which means the URL will not change in the address bar. This in turn means that Wordpress will see "/iframe/" as part of the URL, and tries to find a page or post with that URL. It doesn't find one, so it gives you a 404.
So modify your rule like this:
RewriteRule ^(.*)/iframe/$ /$1?iframe=1 [R,L]
The L flag makes sure that the processing of the rules ends after this rule, then starts over again. This is required to make the R kick in and actually rewrite the URL before it gets to the Wordpress part.
This does mean that ?iframe=1 will remain visible in your URL.

Redirect Spam Query String URLs Created After Hack

One of my sites was hacked and after that thousands of urls were showing up.
I want to edit my htaccess so that all urls that have /?x= are redirected to page without the query string.
The site is a word press site so cant redirect all urls with a query string or the site stops working properly.
Basically I need:
www.example.com/?x=spamurl
to redirect to
www.example.com
and any deep page that includes ?x=
www.example.com/category/?x=spamurl
to redirect to
www.example.com/category/
This is my current htaccess
# 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]
RewriteCond %{QUERY_STRING} x=
RewriteRule ^$ http://www.example.com/? [R=301,L]
</IfModule>
# END WordPress
As you can see I have managed to get any url with ?x= after the main url to redirect to the main page but I cant figure out how to do this when ?x= shows after category pages etc..
Is there coding that can redirect ALL urls that include ?x= to a 404 page without causing an issue for wordpress files?
Many Thanks
Scott
You need to move your redirect rules to before your wordpress rules. You also need to match any incoming URI:
RewriteCond %{QUERY_STRING} ^(.*)(^|&)x=[^&]+(.*)$
RewriteRule ^(.*)$ /$1?%1%3 [R=301,L]
# BEGIN WordPress
etc...
This will remove only the x=something parameter, and if you have other parameters before or after, those are preserved.

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