.htaccess and regular expression remove part of the uri - wordpress

I would like to redirect path like this
http://www.example.com/uploads/2017/10/image.ext/version/?id=87988984632
to
http://www.example.com/uploads/2017/10/image.ext
where extension could be whatever image or video extension.
I'm can detect the last part with this \/version\/(.*)$ but I can't remove from the uri.
Update:
This is my .htaccess and I'm using 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]
RewriteRule \/version\/(.*)$
</IfModule>
Thanks for your help

You can use this new redirect before other WP rules:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.+)/version/?$ /$1? [L,NC,NE,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Related

Wordpress own RewriteRule doesn't work

When I write my.domain/my/url I want to go to other webpage, but I am still on
my.domain/my/url with Error 404
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# Here is my RewriteRule
RewriteRule ^my/url$ //google.pl
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Here is the answer for your htaccess issue. You need to add the folders in the RewriteBase like i did. For example www.domain.com/my/url/ is path of your wordpress then you need to add /my/url/ to rewritebase.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /my/url/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wp/index.php [L]
</IfModule>
# END WordPress

route sub page slug to parent page template wordpress

Currently the .htaccess file 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
What I am trying to do is route all sub page traffic of
/record-detail/what-ever-record
to:
/record-detail/?record_id=what-ever-record
This is taking into consideration there is already a rule for all urls routing to index.php.
Just add the rule before the other wordpress rules like so.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#RewriteRule ^record-detail/([^/]+)/?$ record-detail/?record_id=$1 [QSA,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !^/record-detail [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [QSA,L]
</IfModule>
# END WordPress

.htaccess file for wordpress example.com/blog redirecting to example.com

My website at www.tripfelt.com uses the .htaccess below to handle pretty URLs:
Options +FollowSymLinks
# enable rewrite engine
RewriteEngine On
RewriteRule ^blog - [L,NC]
RewriteRule ^robots.txt - [L]
RewriteRule ^$ index.php?/ [QSA,L]
RewriteCond %{REQUEST_URI} !^/blog
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?/$1 [QSA,L]
www.tripfelt.com/blog uses the following .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]
</IfModule>
# END WordPress
However, clicking on any blog item, instead of opening the blog story, is redirecting to the main site. Any ideas what I am doing wrong?
Try changing the base in your blog folder to /blog/ and remove the leading slash in the routing rule:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
# END WordPress
Solved
Blog folder
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress

.htaccess redirect directory to file

How do you redirect a directory (in this case /resume/) to a file (/Resume.pdf) using an .htaccess file? I have tried different rewrite methods but keep getting a redirect loop. Is this because of a conflict with WordPress?
This doesn't seem to work:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^resume/ http://domain.com/Resume.pdf [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Ok, I figured it out and here's what I got to work:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/resume$ [NC]
RewriteRule ^(.*)$ http://domain.com/Resume.pdf [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

How to avoid a redirect?

I have two similar wordpress websites.
http://site.com/ and http://site.com/en/
When I try to load http://site.com/en/page1.php then it redirects automatically on to http://site.com/page1.php
Seems, I have to change rewrite rule in the htaccess.
http://site.com/ has htaccess with a content whithin:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^en/.*$ en/index.php [L]
#RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The second one is (for http://site.com/en/):
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /en/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Which rule I should write here to work that?
I solved it.
It should be like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /en/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /en/index.php [L]
</IfModule>
# END WordPress

Resources