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]
Related
I'm scratching my head trying to do make a htaccess redirect in case of a cookie is found.
I have several directories /fr-be/, /nl-be/, /en/, ...
When a user visits a page in those directories a cookie named "lang" is dropped with the corresponding directory name so 'fr-be', 'nl-be', 'en', ... as value.
If the user manually type the url : 'https://testme.com' and if the cookie is found it should be redirected to the corresponding dir.
If there is no cookie found, it should be redirected to a default dir '/en/'
If there is something else than '/en/' like https://testme.com/en/test-page/ it should not be redirect.
It is a wordpress that's why there is a ". /index.php [L]" at the end.
I can't use php redirect because the website is on a static (strattic) server
Thanks for you help !
Here is my code :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_COOKIE} lang=fr-be
RewriteRule ^/$ /fr-be/ [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_COOKIE} lang=nl-be
RewriteRule ^/$ /nl-be/ [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_COOKIE} lang=en
RewriteRule ^/$ /en/ [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/$ /en/ [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I'm trying to overwrite only the URLs that end in .html to have the extension removed and then add a trailing slash to the end.
This should result in something like foo.com/bar.html to foo.com/bar/ and still retrieve the html file in the root of the directories.
The below code is working but it can no longer find the .html file to load.
This is on WordPress and I'm thinking this is very important to understand to make this work correctly.
#test (does what it needs to but does not grab the file)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^/]+)/?$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ $1_$2.html [L]
# end test
Don't worry! Simple copy and paste error on RewriteRule ^([^/]+)/?$ $1.php needs to be RewriteRule ^([^/]+)/?$ $1.html
#test (does what it needs to but does not grab the file)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^/]+)/?$ $1.php # Obviously needs to be .html, not .php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ $1_$2.html [L]
# end test
I want to rewrite the following rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(js|ico|gif|jpg|png|css|ttf)$ index.php
Update: I'm looking for an answer without if statement because of performance. if is evil!
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]
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/?$