How to remove specific subdirectories in .htaccess file - wordpress

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

Related

Wordpress not redirecting properly (Forbidden)

At some point I changed my Wordpress URL under Settings -> General. This created a lot of problems that are mostly fixed. However, my site's Url is "example.com/guiacentros/" and that works, but when users wrote "example.com" it also used to work, and now they get a "Forbidden - You don't have permission to access / on this server". Is this an error with the htacces file? This is the current htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /guiacentros/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /guiacentros/index.php [L]
</IfModule>
# END WordPress
I'm a bit confused because there's a second htaccess file one level above, that looks like this:
# 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'm not sure why there's two, but I remember adding the second one fixed some issues.

Wordpress URL - Need to remove a GET parameter

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.

WordPress & .htaccess

I have a website under WordPress where I've created a new page www.foo.com/section/
The idea is to create a permalink like www.foo.com/param1/type/param2 and with .htaccess use a rewrite rule to: www.foo.com/section/?var1=param1&var2=param2&var3=type
RewriteEngine on
Options +FollowSymlinks
RewriteRule ^([a-z0-9_\s-.()&]+)/section1/([a-z0-9]+)$ /section/?var1=$2&var2=$1&var3=section1
RewriteRule ^([a-z0-9_\s-.()&]+)/section2/([a-z0-9]+)$ /section/?var1=$2&var2=$1&var3=section2
I got it working in my old web but in the WordPress crashes.
#My rules -> copy&paste
RewriteRule ^([a-z0-9_\s-.()&]+)/section1/([a-z0-9]+)$ /section/?var1=$2&var2=$1&var3=section1
RewriteRule ^([a-z0-9_\s-.()&]+)/section2/([a-z0-9]+)$ /section/?var1=$2&var2=$1&var3=section2
# 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've only got a beautiful WordPress' 404 page
I've read several questions here but no one helps me.
Thanks for your time and best regards,
R.
13 Jan
I've created a simple test to check it.
The new structure works with a test case without problems:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^lol/$ project/ [L] #Rule 1
RewriteRule ^foo/$ test/ [L] #Rule 2
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Rule 1, I've created a subfolder in WordPress root, named "project" with an index.php echoing "Hi dad" and when type www.foo.com/lol/ it loads that script and echoes the word.
Rule 2, I've created a new WordPress page, www.foo.com/test/
with a simple string to show, but when I write www.foo.com/foo/ it gives me error 404. It seems that the redirection to a wp pages doesn't works as I expected.
Disclaimer: Of course www.foo.com/test/
Now I'm stucked.
Thanks for your previous responses and future help.
Best regards,
R.
14 Jan
I have new news :)
The new .htaccess that works is:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^([a-z0-9_\s-.()&]+)/alternator/([a-z0-9]+)$ ?page_id=554&supplier=$1&reference=$2&ref_type=A [L]
RewriteRule ^([a-z0-9_\s-.()&]+)/starter_motor/([a-z0-9]+)$ ?page_id=554&supplier=$1&reference=$2&ref_type=M [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Instead redirect to "elecfinder/" in the .htaccess I'm pointing to "?page_id=554", at least it doesn't gives me error 440
I'm working on an alternator and starter motors web.
I have a URL that is http://www.elecfinder.com/hitachi/starter_motor/s114850 that really is http://www.elecfinder.com/info/?supplier=hitachi&ref_num=M&reference=s114850, here I load the reference info.
http://wp.elecfinder.com/elecfinder/?reference=s114850&supplier=hitachi&ref_type=M is that web in wp, so my idea is to redirect http://wp.elecfinder.com/hitachi/starter_motor/s114850/ to this.
Now I'm able to redirect http://wp.elecfinder.com/hitachi/starter_motor/s114850 to http://wp.elecfinder.com/elecfinder/?page_id=554&supplier=hitachi&ref_num=M&reference=s114850 but there are 2 problems:
It changes the url to http://wp.elecfinder.com/elecfinder/ instead maintain wp.elecfinder.com/hitachi/starter_motor/s114850
Misses the GET parameters "reference","supplier" and "ref_type"
Thanks for your time.
Best regards,
R.
Try the following:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^([a-z0-9_\s-.()&]+)/(section1|section2)/([a-z0-9]+)$ section/?var1=$1&var2=$3&var3=$2 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Always put your rules after RewriteEngine on.
And, in this case (WordPress), be sure to put your rules before WordPress' rule (it is here to redirect everything to index.php because WordPress has a front controller and it manages rewriting itself).

How do I add additional mod_rewrite rules on a wordpress site?

I have uploaded a file 'events.php' to my wordpress site (the wordpress is installed in my root directory).
I would like to make a mod_rewrite to this file so that one may go to mydomain.com/events
I tried simply doing something like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule %events$ events.php [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Which didn't work.
I also found I could add rewrite tags via wordpress using its inbuilt functions like so:
add_rewrite_rule('^events?','/events.php','top');
This also didn't work - I simply end up on my default 404 page as though its reached a non-existent page within my wordpress.
What am I missing here?
Correct me if I’m wrong, but if all you want is for someone who goes to example.com/events to be directed to example.com/events.php, than you can just do a redirect like so.
Redirect 301 /events http://www.example.com/events.php
Try the following:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^events /events.php [L]
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The directive that you had set for the URL would not match:
RewriteRule %events$ events.php [L]
Also, the directive is now below the condition that checks for an existing file, so that it never matches a direct request for events.php.

htaccess rewriterule

I have a WordPress theme with the following .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
Which removes the "index.php" from the url. But I would like to remove some stuff from my start url, which looks something like "page.com/dir/dir2/page.html", to "page.com". So I tried adding
RewriteRule . /dir/dir2/page.html [L]
after the first rule, but it doesn't seem to work. What do I need to do to fix it?
RewriteRule ^dir/dir2/page\.html$ - [L]
This should work to.
Edit: little big mistake I did there. NOW it should work

Resources