Removing a trailing character at the end of a URL using htaccess - wordpress

I have a url:
www.example.com/tattoo-ink-radiant-colors-teal/
I need it to change to
www.example.com/ink-wineberry/
so far I have
the following in my htaccess as a temporary fix
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ink
RewriteRule -1oz/ /ink [R=301,L]
edit* to make it simpler here are a few more examples
tattoo-ink-radiant-colors-teal-1oz/ should be tattoo-ink-radiant-colors-teal/
tattoo-ink-radiant-super-white-1oz/ should be tattoo-ink-radiant-super-white/

It's ok with:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)-1oz/?$ $1/ [R=301,L]

Related

How to remove last part of URL to avoid 404

I realize there are similar questions but the cases aren't the same. I am looking for a way to remove the last part of a url that no longer exists, but is being linked to from some external sources, to reduce high CPU use. The url would look something like this:
http://example.com/2018/12/05/article-name/removethis
Where removethis is what needs to be deleted to avoid 404 errors. I have seen answers to change a part of the url to another, but not how to simply erase a particular string from the end of the url.
I am also reading htaccess documentation, but it is very extensive and I haven't been able to come up with a way to change a rewrite command from the other examples to do what I need.
Thank you for your time.
I think in this case you should specify what you need in case of error and that is various from one to another one and then go around with specific code to handle it as you want . for example the following code will handle error request level by level but of course it is limited up to /1/2/3/4/5 so , more than this levels will be handled by starting form latest one and so on :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)/(.*)/(.*)/(.*)/(.*)$
RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)$ /$1/$2/$3/$4 [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)/(.*)/(.*)/(.*)$
RewriteRule ^(.*)/(.*)/(.*)/(.*)$ /$1/$2/$3 [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)/(.*)/(.*)$
RewriteRule ^(.*)/(.*)/(.*)$ /$1/$2 [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)/(.*)$
RewriteRule ^(.*)/(.*)$ /$1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule ^(.*)$ / [L,R]
So , the request http://example.com/2018/12/05/article-name/removethis will be http://example.com/2018/12/05/article-name/ if article-name/ directory is exist , otherwise will go to http://example.com/2018/12/05/ if 05/ directory is exist and so on .
Note: the code above could be summarized more by the following form:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)/(.*)/(.*)/(.*)/(.*)$
RewriteRule ^ /%1/%2/%3/%4 [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)/(.*)/(.*)/(.*)$
RewriteRule ^ /%1/%2/%3 [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)/(.*)/(.*)$
RewriteRule ^ /%1/%2 [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)/(.*)$
RewriteRule ^ /%1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule ^ / [L,R]

Subdirectory doesnt work without slash ("/" symbol)

haberimyok.com/tr doesnt work,
haberimyok.com/tr/ works.
.htaccess is in the /web/tr directory
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?haberimyok.com$
RewriteCond %{REQUEST_URI} !^/web/tr
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /web/tr/$1
RewriteCond %{HTTP_HOST} ^(www.)?haberimyok.com$
RewriteRule ^(/)?$ index.php [L]
</IfModule>
Thank you.

Exclude wordpress from .htaccess rules

I have a testing web where there is a testing directory lets say 'test' which contains wordpress directory lets say 'mywordpress', test and his content - excluding the mywordpress - is handled by its own .htaccess - I need the .htaccess to do nothing with the url that goes mydomain.com/test/mywordpressXX (XX can be "/" or "/anything...."
Ive tried something but this .htaccess still works with test content, but throws Internal 500 error when I am trying to reach WP admin:
RewriteEngine On
RewriteBase /test/
DirectorySlash On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)$ $1/ [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/mywordpress/?$
RewriteRule ^([^/?]+)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/mywordpress/?$
RewriteRule ^([^/?]*)/([^/?]*)$ $1.php?$1=$2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/mywordpress/?$
RewriteRule ^([^/?]*)/([^/?]*)/([^/?]*)$ $1.php?$2=$3
What am I doing wrong? Or how is it done correctly ?
Thanks.
Try this .htaccess:
DirectorySlash On
RewriteEngine On
RewriteBase /test/
RewriteRule ^mywordpress(/|$) - [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)$ $1/ [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ $1.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]*)/?$ $1.php?$1=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]*)/?$ $1.php?$2=$3 [L,QSA]
Oh ! Silly me ! I forget about the RewriteBase /test/ you just have to make the exclude rule this way:
RewriteCond %{REQUEST_URI} !mywordpress/?$
There was an additional slash:
RewriteCond %{REQUEST_URI} !/mywordpress/?$

wordpress change to friendly URL with variable using htaccess

I have a wordpress site with a page template and 1 variable call uid (user id) , and I wanna change it to friendly url.
from
http://localhost/forum/edit-profile/?uid=1
to
http://localhost/forum/edit-profile/1
or
http://localhost/forum/edit-profile/uid/1
P/S: wordpress already has a .htaccess file , i just wanna add some additional codes to this.
I tried these solutions, but they are not working:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /forumengine/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /forumengine/index.php [L]
RewrieCond %{QUERY_STRING} !no-redir [NC]
RewrieCond %{QUERY_STRING} uid=([^&]+) [NC]
RewriteRule ^(.*/edit-profile)/$ $1/%1 [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^(.*/edit-profile)/(\d+)$ $1/?uid=$2&no-redir [NC,QSA,L]
</IfModule>
Add this to your .htaccess rules
RewrieCond %{QUERY_STRING} !no-redir [NC]
RewrieCond %{QUERY_STRING} uid=([^&]+) [NC]
RewriteRule ^(.*/edit-profile)/$ $1/%1 [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^(.*/edit-profile)/(\d+)$ $1/?uid=$2&no-redir [NC,QSA,L]
This would redirect /forum/edit-profile/?uid=1 to /forum/edit-profile/1 and back.
EDIT : (with wordpress rules they should go like)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /forumengine/
RewriteRule ^index\.php$ - [L]
RewrieCond %{QUERY_STRING} !no-redir [NC]
RewrieCond %{QUERY_STRING} uid=([^&]+) [NC]
RewriteRule ^(.*/edit-profile)/$ $1/%1 [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^(.*/edit-profile)/(\d+)$ $1/?uid=$2&no-redir [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /forumengine/index.php [L]
</IfModule>

Combine 2 .htaccess Files

I have 2 .htaccess files that I need to merge together. One is generated by wordpress and the other is the existing .htaccess file for the site. The 2 files are as follows:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mywebsite\.com$
RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [R=301]
RewriteRule ^postreview/$ /viewreview.php [NC,PT,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ ./template2.php
<IfModule mod_security.c>
# Turn off mod_security filtering.
SecFilterEngine Off
# The below probably isn't needed,
# but better safe than sorry.
SecFilterScanPOST Off
</IfModule>
php_flag session.use_trans_sid off
2nd file generated by wordpress:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /projectcars/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /projectcars/index.php [L]
</IfModule>
I've tried several ways to combine them, but I either get a redirect error or a internal server error.
Here's my fix:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
RewriteBase /
RewriteRule ^postreview(\/?)$ /viewreview.php [QSA,NC,PT,L]
RewriteRule ^projectcars/index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^projectcars/(.*)$ /projectcars/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) template2.php [NC,L]
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
php_flag session.use_trans_sid off

Resources