htaccess and rewriting urls - .htaccess

currently i have a /en/ folder that is empty except for a .htaccess with the following
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)$ ../index.php?language=en$1 [NC]
i use it to eliminate the need for
index.php?language=en
in all my URLs. I would like to modify the htaccess in a way that i no longer need the /en/ folder with nothing but the htaccess inside. ideally i would like an htaccess in my root folder that reads the url and if it is www.example.com/en/ to rewrite to www.example.com/index.php?language=en

This should work for you:
RewriteRule ^en/(.*)$ index.php?language=en$1 [NC]

Put the following code in .htaccess file in your root folder.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^en/(.*)$ index.php?language=en$1 [L]
Here $1 will append rest of the url as well. The condition will also help if you request your files using direct url.

Related

Rewrite url in htaccess to correct

I have link like this: http://site.crm.int/project_discussions/test
I need redirect it to : http://site.crm.int/project_discussions/index.html?url=test
Text 'test' is changable. Can be for example 'december', 'meetting01' etc.
I tried it in my .htaccess:
RewriteRule ^\/project_discussions/(.*)$ \/project_discussions\/index.html?url=%1 [L]
But have 404 error.
Also tried this:
RewriteCond %{REQUEST_URI} ^/project_discussions/(.*) [NC, L]
RewriteRule ^(.*)$ /index\.html?url=$1 [L,NC]
And have 'Internal Server Error'
How can i fix it? thanks
With your shown samples, please try following .htaccess rules file. Make sure .htaccess file and folder project_discussions are residing in same root folder. Also your index.php OR index.html file should be inside your project_discussions folder.
Make sure to clear your browser cache before testing your URLs.
Also I have fixed your previous rule for handling internal rewrites.
RewriteEngine On
RewriteBase /project_discussions/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ $1/index.html?url=$2 [R=301,NE,QSA,L]

mod_rewrite a sub directoy to child subdirectory

I have a following sub domain and folder structure
https://subdomain.maindomain.com/companyName1/area/dashboard
I want to mod_rewrite to this
https://subdomain.maindomain.com/companyName1/area/
So that everything in "dashboard" loads, in "area"
I would like the .htaccess file to live in the area folder, as there are multiple companyNames
I've tried placing the following .htaccess in companyName1/area
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-s
RewriteCond %{DOCUMENT_ROOT}/dashboard/%{REQUEST_URI} -s
RewriteRule ^(.*)$ /dashboard/$1 [L]
However when visiting https://subdomain.maindomain.com/companyName1/area/ it just loads the index.html of that folder, and not of the /dashboard folder
With your shown samples, attempts please try following htaccess rules. Please make sure to keep these rules at the top of your htaccess file.
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /companyName1/area/
##For external redirect to area link.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s.*/dashboard/?\s [NC]
RewriteRule ^(.*)/dashboard/?$ /$1/? [R=301,L]
##For internal rewrite to dashboard folder here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]

.htaccess prevent of redirect if folder name in url

I have some folders in the root dir with an .htaccess file inside to deny for any access(deny from all). And my htaccess in root is:
RewriteEngine On
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
It's working fine except when I get the folders name in url. I want to ignore folders name and just redirect any url to index.php?url.
Could anyone help?
The !-d, !-f, !-l conditions mean only apply the rewrite rule if the access url does not resolve to an existing file, directory or link. So if the directory exists, it won't apply the rule. You need to remove those. You then need to prevent the recursive rewrite of index.php to itself like so :
RewriteRule ^index.php$ - [L]
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
You probably want to also change .+ in the last rule to .* to also match the empty url.
Indeed it seems the inner .htaccess files are parsed first, before the RewriteEngine one. So you will need to remove those .htaccess and rely on the rewrite rule.

htaccess, How to redirect one directory to one file, another directory to other file?

I have problems using htaccess in WAMP. I need to do the following:
1) redirect URLs like http://localhost/movie_questions/some_number/... to the file http://localhost/movie_questions.php
2) redirect URLs like http://localhost/movie_quiz/some_number/... to the file http://localhost/movie_quiz.php.
3) It's preferable that URLs in rewrite url rules be relative, because I later plan migrating to an Internet server.
Directories 'movie_questions' and 'movie_quiz' do not exist on my server.
Here is my code:
RewriteEngine On
RewriteRule /movie_questions/(.*) /movie_questions.php
RewriteRule /movie_quiz/(.*) /movie_quiz.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Instead of redirecting, I get 404.
You need to remove the leading slash from your RewriteRule's pattern
RewriteEngine on
RewriteRule ^movie_question/(.*)$ /movie_question.php [L]
RewriteRule ^movie_quiz/(.*)$ /movie_quiz.php [L]

htaccess & mod_rewrite: display dynamic php page with a folder

I would want an adress like this:
http://www.domain.com/some_page123/
to display the content of this page:
http://www.domain.com/script.php?var=some_page123
But i want the URL that appears in the browser to be www.domain.com/some_page123/
Is it possible to do that with htaccess even if the folder "some_page123" doesn't really exist ?
Also, i want to exclude the folder named "infos" from this rewriting. So if an user access the URL www.domain.com/infos/ then it will display the content of "infos" folder. I do NOT want to use script.php rewriting for the folder "infos"
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 /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (?!^infos/)^(.+?)/?$ /script.php?var=$1 [L,QSA,NC]
add .htacces in your root directory:
RewriteRule ^script.php - [L,NC]
RewriteRule ^infos/$ - [L,NC]
RewriteRule ^(.*)$ /script.php?var=$1
now I have tested it, it should be working.

Resources