htaccess - Set all EN pages to statuscode 410 - .htaccess

I'm trying to set all EN pages of a website to the statuscode 410. The URLs look like this:
https://example.com/en/terms-and-conditions/
https://example.com/en/sitemap/
https://example.com/en/category/page-1/
https://example.com/en/category/page-2/
https://example.com/en/category/page-3/subpage-1/
I tried different things, but none worked:
RewriteEngine On
RewriteRule ^/en(/.*)?$ - [G,NC]
this also didn't work:
RewriteEngine On
RewriteRule ^/en(.*)$ - [NC,R=410,L]
What am I doing wrong?

When rewrite rules are used in .htaccess the URL path doesn't start with / because that is the current "base" for the rewrite rules. This is different that when rewrite rules are used in Apache's .conf files where the path does start with /.
I recommend writing rules that can be used in either place by making the starting slash optional. You just need to add a ? after the starting slash:
RewriteEngine On
RewriteRule ^/?en(/.*)?$ - [G,NC]

Related

htacces rewrite single file

I want to rewrite a specific file on my website to another one by using htaccess:
# General
RewriteEngine On
RewriteBase /
Options All -Indexes
Options +FollowSymLinks
# Rewrite file
RewriteRule ^/file.html$ /dir/file.html [L]
This is the .htaccess code i'm using based on snippets i found on the internet.
Somehow this is not working, the server is returning a 404-Not-found error.
I can't see any difference with example's that are said to work, like in
Rewriting path for a specific file using htaccess
Edit:
If I place file.html in the root-folder, I can view it. So the rewrite definitely is not happening.
RewriteRule does not receive leading slash. Write so:
RewriteRule ^file.html$ /dir/file.html [L]

htaccess redirect if url contains word on changed url

If my url will like this:
http://id.factor.ua/bez-rubriki/checkout/?price=...
And url will contains bez-rubriki
How I can redirect using .htaccess to this url BUT without this slug, like next URL:
http://id.factor.ua/checkout/?price=...
As simple as this for an internal rewrite:
RewriteEngine on
RewriteRule ^bez-rubriki/checkout/$ /checkout/ [L,QSA]
For an external redirection add the R flag:
RewriteEngine on
RewriteRule ^bez-rubriki/checkout/$ /checkout/ [R=301,L,QSA]
The above are the versions to be used inside .htaccess style files.
A general note: if you have control over the http server configuration, then you should always prefer to place such rules inside the host configuration instead of using .htaccess style files. Those files are notoriously error prone, make things complex, are hard to debug and really slow the server down. They should only be used if there is no control of the http server configuration or if some app requires dynamic changes to the configuration.
So in case you want to place those rules in the host configuration you need a small modification. You have to include the leading slash (/) into the regex testing the request path:
RewriteEngine on
RewriteRule ^/bez-rubriki/checkout/$ /checkout/ [L,QSA]
And the version doing an external redirection:
RewriteEngine on
RewriteRule ^/bez-rubriki/checkout/$ /checkout/ [R=301,L,QSA]

Rewrite URL and remove part with .htaccess

I am really stuck with my .htaccess file, and need some help :). I have a WordPress installation that I am using for testing. It is in a folder and I use .htaccess to get there. This is the rules I use so far:
######### Custom #########
RewriteEngine On
# ignore folders
RewriteCond %{REQUEST_URI} "/af1wp/"
RewriteRule (.*) $1 [L]
###############
# only for me #
###############
# HOME (Senne Tijdeman)
RewriteCond %{REMOTE_ADDR} ^###\.###\.###\.###$
RewriteCond %{HTTP_HOST} ^((www.)?([a-z0-9_\-]+).)?alleenf1.nl$
RewriteCond %{REQUEST_URI} !^/af1wp/$
RewriteRule ^(.*)$ /af1wp/$1 [L]
This works (with my real IP address of course), so no problem there. But now I want to rewrite exisiting URL's to a new format. The old URL is this:
http://alleenf1.nl/nieuws/QOgbb/raikkonen-alles-is-mogelijk-in-australi
The new URL should be this:
http://alleenf1.nl/raikkonen-alles-is-mogelijk-in-australi
The part I want to remove "nieuws/QOgbb/" is not always the same, so I have to use regex for that. But everything I tried did not work at all.
I thought this would be simple, but apparently not for me unfortunately. Now I have 2 questions.
What is the right RewriteRule to do this?
Where should I put it. In the .htaccess of the root folder, or the af1wp folder where the WordPress install is?
Tnx in advanced
To awnser the questions from poncha below:
Yes, the URL's always start with to folders. Just to clarify (was not clear) the part "nieuws" is always the same, but not the second part (call it an ID).
I prefer a redirect.
The file /raikkonen-alles-is-mogelijk-in-australi is a post in WordPress. That WordPress installation currently resides in the folder af1wp, but will be moved to the root folder when going live.
Try this:
RewriteEngine On
RewriteRule ^nieuws/([^/]+)/(.*) /af1wp/$1 [R=301,L,QSA]
This will only match URLs starting with "nieuws"
For now, the rewrite target is /af1wp/, change it to / when moving the wordpress.
When you move wordpress, you'll need to mix in this rule inside the wordpress rules, as it already has rewrite rules of its own - place this rule above its rules.
The flags used here:
R=301 - redirect with HTTP status 301 (Moved Permanently).
L - last rule (stop rules parsing after successful match of this rule)
QSA - query-string-append (append original query string to the rewritten request).

htaccess - rewrite URL to make querystring nice

I have a site with a folder, and a htaccess file within that folder. For the index.php file within that folder, I want to rewrite the querystring for a certain parameter, so that typing in this URL:
www.example.com/myfolder/myparameter
Behaves like this (ie makes $_GET['parameter'] = 'myparameter' in my code)
www.example.com/myfolder/index.php?parameter=myparameter
I have looked at many questions on StackOverflow, but have not managed to get this working. My code so far is
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*) [NC]
RewriteRule ^$ %0 [QSA]
But that just isn't working at all.
Please use this code
RewriteEngine on
RewriteRule (.*) index\.php?parameter=$1 [L,QSA]
RewriteEngine on
RewriteRule (^.*/)([^/]+)$ $1index\.php?parameter=$2 [L,QSA]
update
sorry use #somasundaram's answer. Per-directory .htaccess rewrite rules lose the directory prefix:
When using the rewrite engine in .htaccess files the per-directory prefix (which always is the same for a specific directory) is automatically removed for the RewriteRule pattern matching and automatically added after any relative (not starting with a slash or protocol name) substitution encounters the end of a rule set. See the RewriteBase directive for more information regarding what prefix will be added back to relative substitutions.
(from the apache docs)

My url rewrite redirects to mydomain.com/var/www/clients/client3/web25/

I'm trying to make a rewrite of a url on my joomla site. I have an extra page in there wiht some other stuff on it.
I have:
RewriteRule ^company/([^/]+) company?alias=$1 [NC]
where i want company/somecompany to show what the url company?alias=somecompany will give (not forward to it).
What i get instead is a redirection to:
http://www.mydomain.dk/var/www/clients/client2/web5/web/company?alias=somecompany
Also if i include a "-" like "company/some-company" it skips my rewrite and just goes to the joomla rewrite rules (and can't find the article)
What am I doing wrong?
You need to add a RewriteBase. When your rewrite rule's target doesn't start with a leading slash (making it an absolute URI), apache needs to guess whether it's a URI-path or a file-path, and it is incorrectly guessing that it's a file-path. Adding a base tells apache that it's a URI path.
RewriteBase /
or add a leading slash to your target: /company/?alias=$1
You also want to add a trailing slash to company and use the [L] flag:
RewriteRule ^company/([^/]+) company/?alias=$1 [L,NC]
The missing trailing slash may be the culprit that's causing mod_dir and DirectorySlash redirecting. And the L flag may be why the joomla rules are eventually getting applied.
Here is the working example:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^/?old-folder/(.*)$ /new-folder/$1 [R=301,L]
</IfModule>
For more examples, check:
Learn Apache mod_rewrite: 13 Real-world Examples
Apache Conf Snippets for VS Code

Resources