Wordpress URL - Need to remove a GET parameter - wordpress

I know there are a lot of threads for .htaccess URL rewriting, but my case seems to be a bit different and I have tried a lot but it doesn't work.
My current URL: http://example.com/forest/trees/?type=perennial
What I need is: http://example.com/forest/trees/perennial
I just need to remove the ?type= from the URL.
EDIT: The URL may contain hyphens - between strings at any point (except the domain name ofcourse). It can be dense-forest or non-perennial too.
It's a custom code and plugin, so can't modify it. I just need the URL beautified.
Any help would be greatly appreciated.
What I've tried so far in .htaccess:
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /forest\/trees\/\?type=([^&]+)
RewriteRule ^ \/forest\/trees\/%2\/? [L,R=301]
and
RewriteRule ^\/forest\/trees\/([^/]*)? /forest/trees/?type=$1 [L]
My current Wordpress .htaccess is:
# 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
Thanks in advance. :)
Cheers!

You can use this .htaccess file:
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^forest/trees/([^/]+)$ /forest/trees/?type=$1
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
Now URLs like http://example.com/forest/trees/perennial will internally redirect to /forest/trees/?type=perennial and then to Wordpress Dispatcher /index.php.

Related

How to remove specific subdirectories in .htaccess file

Even after extensive research I was unable to resolve following redirection. I tried tons of examples, but with no luck.
From this URL:
http://www.example.com/store/product/name
I would like to get:
http://www.example.com/name
Problem is that the only known string is "store". Other strings "product" and "name" could contain letters, numbers and "-" or "_" characters.
I am testing it on live website and also on this tester: htaccess.madewithlove.be
One of the examples I used:
RewriteEngine On
RewriteBase /
RewriteRule ^store/([^/]+)/([^/]+) $2 [L,R=301]
This seems to work on above tester website, but not in real environment. I think there is something wrong with the regexp and second parameter is not properly returned or I am missing something else.
I should also mention that the website runs on Wordpress and in .htaccess file there is also following section created automatically by Wordpress:
# 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
Thanks for your help!
Have your rule like this i.e. redirect before internal WP rule:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^store/([^/]+)/([^/]+) $2 [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Redirect 301 all files from a folder to one file?

Good day everyone,
On my Wordpress website, I had a plugin that transfered external links through virtual site.com/go/abc123xyz style links.
I have removed the plugin but obviously, when I checked G webmaster tools, there were many Not Found errors for all of those virtual links.
All I wish to do is redirect all of those /go/links to one particular external link. I'm looking for a redirection rule which will redirect all links from the virtual /go/ folder to my specified link.
Would be really thankful if anyone can help me. Thanks!
Try adding:
RewriteRule ^go/(.*) www.mysitedotcom/out/link.php?id=$1 [R=301,NC,L]
To your default htaccess file:
# 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
So it should look like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^go/(.*) /out/link.php?id=$1 [R=301,NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I hope that answers your question and helps you.

Adding 410 to page in .htaccess in Wordpress

I want to ask is it correct the way that I add 410 to page in .htaccess in Wordpress
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule http://exapmle.com/%D1%8BB%D1%83%D0%B3%D0%B8/ [G=410,L]
RewriteRule http://example.com/%D0%%D1%82%D0%B0%D0%BA%D1%82%D0%B8/ [G=410,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Thank you!
you don't want the http://example.com part in the pattern, that's not part of the URI that's used for the pattern to match against
You don't need the =410 in the flags
The URI is already decoded by the time mod_rewrite rules get applied, so you don't want (I'm assuming) the URL encoded string.
Try:
RewriteRule ^ыBуги/ - [G,L]
RewriteRule ^ÐÑакÑи/ - [G,L]
If you want to show "Gone" error for those 2 URLs (mentioned in your code) then put this code right on top of .htaccess after RewriteBase line:
RewriteRule ^\xD1\x8BB\xD1\x83\xD0\xB3\xD0\xB8/?$ - [G,L]
RewriteRule ^\xD0\xD1\x82\xD0\xB0\xD0\xBA\xD1\x82\xD0\xB8/?$ - [G,L]
Do you mean:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/%D1%83%D1%81%D0%BB%D1%B3%D0%B8/ - [G,L]
RewriteRule ^/%D0%B7%D0%0%BE%D0%BD%D1%82%D0%B0%D0%BA%D1%82%D0%B8/ - [G,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPdpress
or to put these two lines of URLs at the top ot the .htaccess file, outside of:
# BEGIN WordPress

Redirect of URL with parameters to URL without -- plus Wordpress

I have rebuilt a website that used some custom php to serve up individual store pages with urls like this:
http://thedomain.com/storedetails.php?storeNum=1
The new site is powered by Wordpress, and each individual store will live in a named subdirectory, like this:
http://thedomain.com/stores/gothamcity
I have been trying solutions I have found on stackoverflow that all seem to be variations on this: 301 Redirect of old url with parameters to a path without parametes
Here's an example:
RewriteCond %{QUERY_STRING} ^storeNum=18$ [NC]
RewriteRule ^/storedetails\.php$ http://www.thedomain.com/stores/gothamcity? [L,R=301]
But so far nothing works. I continue to get "page not found" 404 error pages in Wordpress.
My best guess is that something in Wordpress's part of the .htaccess is throwing this off? Here's what they include:
# 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
Can anyone help?
This should work:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^storeNum=18$ [NC]
RewriteCond %{HTTP_HOST} .*
RewriteRule ^storedetails\.php http://www.thedomain.com/stores/gothamcity? [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I suggest you use a RewriteMap though to map store numbers to names.

Add URL Rewrite Rule To Wordpress

I've written a Wordpress plugin that adds a query string to the URL. However I can't seem to modify the htaccess to rewrite this. Not sure if Wordpress is overriding it?
The current htaccess is:
# 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
And the URL I'm trying to rewrite is:
http://domain.com/deal-info/?id=87&post_name=testdealtitle
Desired URL:
http://domain.com/deal-info/87/testdealtitle
I've tried adding:
RewriteRule ^([^/]*)/([^/]*)$ /deal-info/?id=$1&post_name=$2 [L]
to no avail. Any ideas appreciated!
When you do this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
This means: "if it's not a file, and if it's not a dir, redirect all incoming URLs to
index.php.
Your rewriterule should work... unless you've put it after the previous rewriterule. So, in short, are you sure your .htaccess it like that:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]*)/([^/]*)$ /deal-info/?id=$1&post_name=$2 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Please tell me if it works.
Moreover if you only want to redirect only deal-info, you should do something like (not tested):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(/)?deal-info/([^/]*)/([^/]*)$ /deal-info/?id=$2&post_name=$3 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Two hints:
Please try to use the RewriteLog directive: it helps you to track down such problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)
May I ask you to add the rewritelog in your question?
After much confusion I found an answer here:
https://wordpress.stackexchange.com/questions/5413/need-help-with-add-rewrite-rule
Though I think the proper way to do it is by:
http://codex.wordpress.org/Rewrite_API/add_rewrite_rule
I'm not sure if you can do this directly in htaccess, or I was having a conflict problem with another plugin I was using.
Thanks again to Olivier for his extensive answer!

Resources