RewriteRule same name as directory - .htaccess

For my site I have a directory called /test/. I want to rewrite www.example.com/nl/test and www.example.com/nl/test/ to a certain page (test.php).
Some global conditions (for all the rules)
RewriteRule ^(nl|en)$ http://www.example.com/$1/ [NC,R]
RewriteBase /
RewriteRule ^(nl|en)$ $1/ [NC,R]
RewriteCond $1 !^(en|nl)$
RewriteRule ^([a-z]{2})/(.*)$ en/$2 [L,R=302]
RewriteRule ^(nl|en)/(.*)$ $2?language=$1&%{QUERY_STRING} [L]
RewriteRule ^sale$ sale.php
RewriteRule ^valentine$ valentine.php
Some conditions for the rewrite + folder
RewriteRule ^test/$ test.php
The redirect of www.example.com/nl/test/ is correct. The language parameter is also correctly rewritten.
For the second redirect (the version without the trailing slash) I can't get this working.
RewriteRule ^test$ test.php
Now my URL is rewritten as www.example.com/test/?language=nl
Can someone give me a tip or hint to fix this? I can't change the name of the directory since there are several external URLs linking to this directory.

This rule will do the whole job (instead of 4 lines you have there): it will rewrite both /nl/test and /nl/test/ to /test.php?language=nl.
RewriteRule ^(en|nl)/test/?$ /test.php?language=$1 [NC,QSA,L]
NOTES:
The [QSA] flag will preserve any existing query string (therefore, there is no need for &%{QUERY_STRING}).
Full .htaccess:
Options +FollowSymLinks -MultiViews
DirectorySlash Off
RewriteEngine On
RewriteBase /
RewriteRule ^(nl|en)$ http://www.example.com/$1/ [NC,R=301,L]
RewriteCond $1 !^(en|nl)$
RewriteRule ^([a-z]{2})/(.*)$ /en/$2 [R=302,L]
RewriteRule ^(nl|en)/(.*)$ /$2?language=$1 [NC,QSA,L]
RewriteRule ^sale/?$ sale.php [QSA,L]
RewriteRule ^valentine/?$ valentine.php [QSA,L]
RewriteRule ^test/?$ test.php [QSA,L]
NOTES:
There is no need for RewriteRule ^(nl|en)$ $1/ [NC,R] as you already have RewriteRule ^(nl|en)$ http://www.example.com/$1/ [NC,R=301,L]. It does the same job.

Related

Hiding multiple dynamically-named subdirectories with mod_rewrite in .htaccess

I am having difficulty configuring a mod_rewrite rule in my .htaccess file to hide certain subdirectories.
Input:
https://example.com/hidden_sub1/hidden_sub2/additional_sub/file.php?lang=en-US
Desired output:
https://example.com/en-US/additional_sub/file
I had code that allowed me to hide something if I specifically name it:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET\ /hidden_sub1/hidden_sub2/ [NC]
RewriteRule ^hidden_sub1/hidden_sub2/(.*) /$1 [L,R=301]
RewriteRule !^hidden_sub1/hidden_sub2/ hidden_sub1/hidden_sub2%{REQUEST_URI} [L]
However, it stopped working once I added in the following language subdirectory code:
RewriteRule ^([a-z]{2}(-[A-Z]{2})?)/(.*) $3?lang=$1 [L,QSA]
Besides, the name of the subdirectory is dynamic. "hidden_sub1" might be "random_name243". However, they would always be the first two subdirectories.
So how do I take out the first two randomly-named subdirectories while still keeping the language subdirectory?
I appreciate any help.
To change your url from the form
https://example.com/hidden_sub1/hidden_sub2/additional_sub/file.php?lang=en-US
To the form :
https://example.com/en-US/additional_sub/file
You can use the following Rewrite rule
RewriteEngine On
RewriteBase /
#1)externally redirect "/hidden_sub1/hidden_sub2/foo.php?lang=bar" to "/bar/foo"
RewriteCond %{THE_REQUEST} /hidden_sub1/hidden_sub2/([^.]+)\.php\?lang=(.+)\sHTTP [NC]
RewriteRule ^ /%2/%1? [L,R=301]
#2)internally rewrite "/bar/foo" to "/hidden_sub1/hidden_sub2/foo.php?lang=bar"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ /hidden_sub1/hidden_sub2/$2.php?lang=$1 [NC,L]
Tested on apache 2.2. and 2.4.

.htaccess url rewrite with two variables

i tried and tried with examples posted here, but i didn't manage to make my htaccess run properly.
Here's the situation:
i have links looking like this
domain.com/sport/football/index.php?lang_id=1&page_id=500 (home page)
domain.com/sport/football/index.php?lang_id=1&page_id=505 (players)
domain.com/sport/football/index.php?lang_id=1&page_id=510 (coaches) ...
i would like to rename them to
domain.com/sport/football/
domain.com/sport/football/players/
domain.com/sport/football/coaches/
etc...
and for all non-designated page_id's to redirect to home page.
All help is very much appreciated.
In the htaccess file in your document root, add:
RewriteEngine On
RewriteRule ^sport/football/$ /sport/football/index.php?lang_id=1&page_id=500 [L,QSA]
RewriteRule ^sport/football/players/?$ /sport/football/index.php?lang_id=1&page_id=505 [L,QSA]
RewriteRule ^sport/football/coaches/?$ /sport/football/index.php?lang_id=1&page_id=510 [L,QSA]
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /sport/football/index\.php
RewriteCond %{QUERY_STRING} ^(.*)page_id=500($|&)
RewriteRule ^ /sport/football/? [L,R=301]
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /sport/football/index\.php
RewriteCond %{QUERY_STRING} ^(.*)page_id=505($|&)
RewriteRule ^ /sport/football/players/? [L,R=301]
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /sport/football/index\.php
RewriteCond %{QUERY_STRING} ^(.*)page_id=510($|&)
RewriteRule ^ /sport/football/coaches/? [L,R=301]
You can use RewriteMap Directive for that. You must define the mapping from names to ids
players 505
coaches 510
and tell Apache about the map
RewriteMap football txt:/path/to/footballmap.txt
The RewriteMap must be in either the main configuration file or inside a VirtualHost directive.
Now you can use this map
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sport/football/(.*)/?$ /sport/football/index.php?lang_id=1&page_id=${footballmap:$1|500} [L]
If there's no key found, the default 500 (homepage) will be used. If you have lots of mappings, you can also use a hashfile instead.
Update:
When you don't have access to the server or virtual host configuration file, you can only have a fixed RewriteRule "map"
RewriteRule ^sport/football/players/?$ /sport/football/index.php?lang_id=1&page_id=505 [L]
RewriteRule ^sport/football/coaches/?$ /sport/football/index.php?lang_id=1&page_id=510 [L]
# maybe other similar rules ...
# this is a catch everything else and must come last
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sport/football/ /sport/football/index.php?lang_id=1&page_id=500 [L]

htaccess similar file names goes to same page

Most of the RewriteRules are working fine but there are a couple that have the same word in them and aren't going to the correct page.
Here is my full htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
# Rewrites for main pages
RewriteRule home index.php
RewriteRule about-us about-us.php
RewriteRule business-advice business-advice.php
RewriteRule associates associates.php
RewriteRule become-an-associate associates-sign-up.php
RewriteRule blog blog.php
RewriteRule contact-us contact-us.php
RewriteRule log-in log-in.php
RewriteRule sign-up sign-up.php
The problem resides within the two associated links. When I go to [MYURL]/associates it works fine but if I go to [MYURL]/become-an-associate it takes me to the correct URL but shows content from [MYURL]/associates
Anybody got any ideas?
Thanks,
The pattern for RewriteRule's are regular expressions and the rules all loop until the URI stops changing. That means the first time around, when you request /become-an-associate, it matches and is rewritten to /associates-sign-up.php. Then, the second time around, the rule with the pattern associates matches because of "/associates-sign-up.php". You need to add boundary checks (e.g. ^ and $) as well as the [L] flag:
# Rewrites for main pages
RewriteRule ^home/?$ index.php [L]
RewriteRule ^about-us/?$ about-us.php [L]
RewriteRule ^business-advice/?$ business-advice.php [L]
RewriteRule ^associates/?$ associates.php [L]
RewriteRule ^become-an-associate/?$ associates-sign-up.php [L]
RewriteRule ^blog/?$ blog.php [L]
RewriteRule ^contact-us/?$ contact-us.php [L]
RewriteRule ^log-in/?$ log-in.php [L]
RewriteRule ^sign-up/?$ sign-up.php [L]

301redirect to remove folder from URL

I have:
mydomain.com/folder-name/segment1/segment2
I want to change it to:
mydomain.com/segment1/segment2
using a 301 redirect.
I've tried:
RewriteCond %{REQUEST_URI} !^/test/.*$
RewriteRule ^(.*)$ /test/$1 [L]
but its not working
here is my htacess file:
# #AddHandler application/x-httpd-php53 .php .php5 .php4 .php3
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/b1/.*$
RewriteRule ^(.*)$ /b1/$1 [R=301,L]
The answer for the first part of the question should be like this:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/? $2/$3 [R=301,L]
The second code that you've tried is the opposite of what you're asking for initially. This line matches anything not starting with /test/:
RewriteCond %{REQUEST_URI} !^/test/.*$
This line says take everything and rewrite it to the /test/ directory:
RewriteRule ^(.*)$ /test/$1 [L]
So together anything that's not in the test directory is being rewritten to the test directory.
If you're trying to specifically remove the word test then you would remove the ! symbol in your attempt to create a match. Since you already know it's called test there's no need to even make Apache perform this look for 'test' because Apache handles the RewriteCond statement after the RewriteRule (rather unintuitively).
RewriteCond %{REQUEST_URI} ^/?test
You can specialize the rewrite rule like this (I've added [QSA] to catch any query strings:
RewriteRule ^test/([^/]+)/([^/]+)/? $1/$2/ [R=301,L,QSA]
Simply change your code to:
RewriteRule ^test/(.*)$ /$1 [R=301,L,NC]

Add Trailing Slash to URL

My current .htaccess file looks like this:
RewriteEngine on
RewriteBase /
Options +FollowSymLinks -Indexes
RewriteRule ^video/(.*)$ video.php?id=$1 [L]
RewriteRule ^video/(.*)$([a-zA-Z0-9]+) video.php?id=$1 [L]
RewriteRule ^tag/(.*)/page-(.*)/$ tag.php?tag=$1&page=$2 [L]
RewriteRule ^tag/(.*)/page-(.*)$ tag.php?tag=$1&page=$2 [L]
RewriteRule ^tag/(.*)?$ tag.php?tag=$1
RewriteRule ^page/(.*)$ page.php?id=$1 [L]
RewriteRule ^feed feed.php [L]
i want to add a slash to all my url's
like this:
> example.com/video/video_id/
>
> example.com/tag/keyword/
>
> example.com/tag/keyword/page-2/ (3... and so on...)
>
> example.com/page/name/
>
> example.com/feed/
And i want to redirect my current links to the new slash url's
Can somebody help me, please?
Your current htaccess file supports a trailing / although you probably would prefer
RewriteRule ^video/(.*)/$ video.php?id=$1 [L]
So that you don't have to handle the / in video.php
Just update all of your URLs to be example.com/video/video_id/ instead of example.com/video/video_id in whatever you are using (your framework/flat HTML files).
Your old URLs will still work. If you really want to redirect them, you can:
RewriteCond %{REQUEST_URI} ^video/ [NC]
RewriteRule ^video/(.*)$ video.php?id=$1 [L,R=301]
The [NC] means No-case checking (so /VIDEO) would work. The [R=301] means permanent redirect (useful for SEO).
Go through and expand for your other rules.
Edit:
Sorry, I don't think it was quite right before. Try the following:
RewriteEngine on
RewriteBase /
Options +FollowSymLinks -Indexes
RewriteCond %{REQUEST_URI} ^video/ [NC]
RewriteRule ^video/(.*)$ video/$1/ [L,R=301]
RewriteRule ^video/(.*)/$ video.php?id=$1 [L]
RewriteCond %{REQUEST_URI} ^tag/ [NC]
RewriteRule ^tag/(.*)/page-(.*)$ tag/$1/page-$2/ [L,R=301]
RewriteRule ^tag/(.*)/page-(.*)/$ tag.php?tag=$1&page=$2 [L]
...

Resources