Issue with specific 301 redirection in htaccess - apache

I'm having an issue with a format of URL.
I need to send
/en
to /
The problem is that I need only /en (exactly), not /en/foo...
I know how to do it with string that ends with .html, but here I have other URLS that have /en/stuff and they are also matched.
Would really appreciate assistance...

You need to specify end of string in RewriteCond e.g.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/en$
RewriteRule ^(.*)$ / [L,R=301]
Please note that Condition pattern as well as Rewrite Rule pattern are a perl compatible regular expression (with some additions):
For Anchors:
^ - Start-of-line anchor
$ - End-of-line anchor
EDIT
BTW, there is very nice htaccess tester at http://htaccess.madewithlove.be/ which you could use to test the rules.

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^en/?$ / [L,R=301,NC]
This will redirect /en or /en/ to / also note that /EN will also be handled because of NC (Ignore Case) flag used here.

Related

Redirect with RewriteRule apache

I want to make an internal redirect with this result:
My old link is like: http://www.example.com/en/some-other-things
My new link should be: http://www.example.com/some-other-things
Basically I want to 'remove' the /en/ and get all the rest of the url in the new url. I've tried this rules in the .htaccess without success:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule "^/en/(.+)" "/$1" [R=301,L]
</IfModule>
Make leading slash optional so that rule works both in Apache config as well in .htaccess files. Use .* instead of .+ to ensure you match example.com/en/ as well. Also there is no need to quote the pattern or target.
RewriteEngine On
RewriteRule ^/?en/(.*)$ /$1 [R=301,L,NC,NE]

htaccess - Simple rewrite rule not triggering

I'm trying to match a simple rule to rewrite a url but it's just not matching. I want to redirect
https://example.com/web/thanks/
to
https://example.com/thanks.php
Here's what I've tried
RewriteEngine On
RewriteBase /
RewriteRule ^thanks/$ https://example.com/thanks.php [R=302,L]
RewriteEngine On
RewriteRule ^thanks/$ https://example.com/thanks.php [R=302,L]
RewriteEngine On
RewriteBase /
RewriteRule ^/(.*)/thanks/$ https://example.com/thanks.php [R=302,L]
RewriteEngine On
RewriteBase /
RewriteRule ^web/thanks/$ https://example.com/thanks.php [R=302,L]
and many more tiny variations but none of them are triggering. I tried using this online tool and it returns "This rule was not met". What am I doing wrong?
To rewrite, just use your last rule
RewriteRule ^web/thanks/?$ /thanks.php [L]
with the following changes
no RewriteBase, this is only relevant for some relative URLs
optional trailing slash /?, if you want both /web/thanks or /web/thanks/ to work
no domain name, because this might trigger a redirect instead of a rewrite, see RewriteRule
Absolute URL
If an absolute URL is specified, mod_rewrite checks to see whether the hostname matches the current host. If it does, the scheme and hostname are stripped out and the resulting path is treated as a URL-path. Otherwise, an external redirect is performed for the given URL. To force an external redirect back to the current host, see the [R] flag below.
no R|redirect flag, because this triggers a redirect instead of a rewrite
The pattern ^.*thanks/$ or ^(.*)thanks/$ also works, but it matches any URL ending in thanks/, like /hellothanks/, /areyousurethanks/, /some/path/thanks/, ...

My .htaccess rule is not working

I want all my URL with matching pattern as
/released/2013/iron-man
/released/2013/abc-of-death
/released/2012/saw6
to be redirected to
/released/1.php
and I can the name as well.
I am adding this rule to my .htaccess file but its not working
RewriteRule ^released/([0-9]+)/?$ /released/1.php?id=$1 [L]
The trailing question mark matches an optional ending / which is not what you want.
^released/([0-9]+)/iron-man$
or
RewriteRule ^released/([0-9]+)/(.+)$ /released/1.php?id=$1+$2
Problem is that you have $ after second slash but you have movie name after 2nd slash like iron-man etc. Remove $ since you are not matching it.
Make sure that mod_rewrite and .htaccess are enabled through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(released)/([0-9]+)/ /$1/1.php?id=$2 [L,QSA,NC]
A RewriteRule which does not specify a specific external domain is always executed internally. To make it redirect as you ask add [R=301] at the end - or 302, 303 or 307 depending on which kind of redirect you require, but usually 301 is fine.
Besides that, the regular expression you wrote does not allow for extended URLs - remove the trailing $. After that the /? part is moot so you can remove it as well.
The resulting line would read:
RewriteRule ^released/([0-9]+) /released/1.php?id=$1 [L,R=301]

Simple modrewrite to remove particular part of path

I'm trying to create a modrewrite rule that will change:
/blah/correct/xyz.htm
to
/correct/xyz.htm
There is not always /blah but when it's there, it always appears at the beginning of the URL. The URL can be any length, with numerous sub-paths. It could even be /blah/myfile.htm (which should just rewrite to /myfile.htm).
What's the best way to do this?
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule (?:^|/)blah(/.+)$ /$1 [L,NC]
This will internally forward /blah/foo to /foo or /blah/correct/foo to /correct/foo. If you want external rewrite then use:
RewriteRule (?:^|/)blah(/.+)$ /$1 [L,R=301,NC]

.htaccess redirection from multiple directories to a single page

I have a single blog.php page that should get redirected from all these requests:
www.site.com/blog #goes to blog.php
www.site.com/blog/id-of-an-entry #goes to blog.php?id=id-of-an-entry
Also internationalised versions such for french:
www.site.com/fr/blog *# to blog.php?lang=fr
www.site.com/fr/blog/id-of-entry* #to blog.php?lang=fr&id=id-of-entry
What would be the more eficient and effective cond/rules for .htaccess? I've made many attemps but end walking in circles or with to many specialised rules :-) Thanks for any insights!
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^blog/([^/]+)/?$ /blog.php?id=$1 [L,NC,QSA]
RewriteRule ^blog/?$ /blog.php [L,NC,QSA]
RewriteRule ^([^/]+)/blog/([^/]+)/?$ /blog.php?lang=$1&id=$2 [L,NC,QSA]
RewriteRule ^([^/]+)/blog/?$ /blog.php?lang=$1 [L,NC,QSA]
I am not using this regulary so there can be better way but this could help you. Rules works like this. In () are your variables which corresponds to what you want to match. Than you can refer to them by $1 and so on. For example news/local would redirect them to file news-local.php you can use the same system to match you get variables.
RewriteRule ^([a-z]+)/([a-z]+)$ $1-$2.php