htaccess RewriteRule redirecting to parent directory? - .htaccess

I cant understand how to redirect to the parent directory in this case:
I have a root directory with .htaccess file, where rewriteEngine is on. In this directory I have a file rootFile.php.
Also, in this directory I have a directory "forum", with another .htaccess file in it.
When there is request to "forum.mySite.com/rootFile.php", I need to redirect it to "mySite.com/rootFile.php" which means I need to redirect request to the parent directory.
/forum/.htaccess:
RewriteEngine on
RewriteBase /forum/
RewriteRule ^sitemap /forum/map.php [L]
...
I tried to:
RewriteEngine on
RewriteBase /forum/
RewriteRule ^rootFileName ../rootFile.php [L]
\# or so RewriteRule ^rootFileName /rootFile.php [L]
\# or so RewriteRule ^rootFileName rootFile.php [L]
RewriteRule ^sitemap /forum/map.php [L]
Also tried this one:
RewriteEngine on
RewriteBase /
RewriteRule ^rootFileName ../rootFile.php [L]
\# or so RewriteRule ^rootFileName /rootFile.php [L]
\# or so RewriteRule ^rootFileName rootFile.php [L]
RewriteBase /forum/
RewriteRule ^sitemap /forum/map.php [L]
And it always redirect from /forum/rootFileName to /forum/rootFile, but never to /rootFile.
Where is my mistake?

You can't rewrite to outside the document-root. This is a security thing.
You could just create a rootFile.php that only contains <?php include('../rootfile.php'); ?>

You can't rewrite to paths that are outside your document root for security reasons. In fact this should be avoided because it is not a good practice.
Anyway, to answer your question, a workaround might be creating a symlink in your document root that points to the target/parent directory, but you should protect it from direct accesses.
Let me do an example. Assume that our document root is /var/www/project1 and that you want to rewrite on /var/www/project2/target.php
Then, you have to create a symlink inside /var/www/project1 that points to /var/www/project2 named (for example) p2.
This should be your /var/www/project1/.htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} /hello/world
RewriteRule ^(.*)$ p2/target.php [QSA,L] # p2 is the symlink name!
</IfModule>

You wrote:
When there is request to "forum.mySite.com/rootFile.php", I need to
redirect it to "mySite.com/rootFile.php" which means I need to
redirect request to the parent directory.
Did you mean:
When there is request to "mySite.com/forum/rootFile.php", I need to
redirect it to "mySite.com/rootFile.php" which means I need to
redirect request to the parent directory.
...Which in turn means:
When there is request to "/forum/rootFile.php", I need to
redirect it to "/rootFile.php" which means I need to
redirect request to the parent directory.
In your .htaccess in the /forum dir try this:
RewriteEngine On
RewriteRule ^rootFileName(.*) /rootFile.php [QSA,R=301,L]

Related

RewriteRule php to html extension

how to write RewriteRule to access php file as html extension, the mark file is located inside folder called domain.com/markfolder/mark.php
and i got following in my root directory
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RedirectMatch 301 (.*)\.html$ $1.php
and below file i try to locate inside domain.com/markfolder/.htaccess
try 1
RewriteEngine on
RewriteRule ^mark\.php$ mark.html [NC,R]
try 2
RewriteEngine on
RewriteRule ^(.*)\.html $1\.php
try 3
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^(.+)\.html$ $1.php [NC,L]
above three try simple doesn't work.
First, your htaccess file in your root directory is causing the problem. It's redirecting .html to .php and is part of mod_alias, not mod_rewrite. This means requests will get processed by both the RedirectMatch as well as any rewrite rules. So first change your root htaccess to:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\.html$ $1.php [L,R=301]
Not sure why you want to permanently redirect, since this will affect all requests even requests in subdirectories. It seems like it's not what you want and will only break any attempt for you to rewrite.
Then in the htaccess file in your markfolder:
RewriteEngine On
RewriteRule ^(.*)\.html$ $1.php [L]

Redirect url with .htaccess

I m sure that many people will say that this is duplicated but I try everything from other "Question"`s and nothings work for me.
The problem is that I move my project to the web server.
In this server I have folder "public_html" where I but my project Symfony.
Now to enter on my project I must write the following url: www.mydomain.com/Symfony/web/*
But I want to write a Rewrite Rule which will redirect from www.mydomain.com/Symfony/web/* to
www.mydomain.com/home/*.
To do this I try on 2 different ways with many combination of ReWrite rule.
In my public_html I create a .htaccess folder
I edit the .htaccess in Symfony/web folder
I add the following rule in both file but without success
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^Symfony/web/(.*)$ www.mydomain.com/home/$1 [L,R=301]
Unfortunately without success. What I`m doing wrong?
My htaccess file look like
And all the time Error 404 Object not found
Symfony/web/.htaccess
DirectoryIndex app.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Symfony/web/
RewriteCond %{THE_REQUEST} \s/+Symfony/web/ [NC]
RewriteRule ^(.*)$ /home/$1 [L,R=301]
RewriteRule ^home/(.*)$ $1 [L,NC]
</IfModule>
It`s redirecting me but I receive again Object not found :(
I delete the .htaccess in public_html folder which is the root one for my server
public_html\.htaccess
RewriteEngine On
RewriteRule ^home/(.*)$ /Symfony/web/$1 [L,NC]
1: Place this code in /Symfony/web/.htaccess:
RewriteEngine On
RewriteBase /Symfony/web/
RewriteCond %{THE_REQUEST} \s/+Symfony/web/ [NC]
RewriteRule ^(.*)$ /home/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [L]
2: Place this code in /public_html/.htaccess:
RewriteRule ^home/(.*)$ /Symfony/web/$1 [L,NC]
I'm going to go out on a limb and say that your rule is backwards. I think you want your URL to be www.mydomain.com/home/* in the browser... In which case the rule would be reversed. Also, your .htaccess should be in the root and you don't need to include the domain in the rewrite rule because you set a rewrite base.
RewriteRule ^home/(.*)$ Symfony/web/$1 [L,R=301]

Mod_Rewrite with Relative Path Redirects

I have this rule in an .htaccess file located in a directory named clips/:
RewriteRule ^mlk/?$ segment/index.php?clip=1 [R=301,QSA,L]
What I am intending is that when someone visits http://example.local/clips/mlk they are redirected to http://example.local/clips/segment/index.php?clip=1
What actually happens is that when someone visits example.local/clips/mlk they are redirected to example.local/var/www/example/clips/segment/index.php?clip=1
I'm not sure why it's doing this. If I change the rewrite rule to this:
RewriteRule ^mlk/?$ /segment/index.php?clip=1 [R=301,QSA,L]
The user is redirected to example.local/segment/index.php?clip=1, which is still incorrect. I don't want to have to specify an absolute path in the case of these files being moved around the website's directory tree. How can I get this to work relatively instead of absolutely?
Try adding a RewriteBase directive as below
RewriteEngine On
RewriteBase /clips/
RewriteRule ^mlk/?$ segment/index.php?clip=1 [R=301,QSA,L]
EDIT
but is there any way to get this to work without using a RewriteBase directive
You could also try
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(/[^/]+/) [NC]
RewriteRule ^mlk/?$ http://%{HTTP_HOST}%1segment/index.php?clip=1 [R=301,QSA,L]

.htacces rewriterule to redirect if .exe/.zip

I am trying to write RewriteRule to redirect www.myweb.com/download/blabla.zip to > www.myweb.com/download/download.php?filedownload=blabla.zip
this is my .htaccess
Options +FollowSymlinks
RewriteBase /download/
RewriteEngine on
RewriteRule ^(.*)$ download.php?filedownload=$1 [L]
the .htaccess located in the folder /download
and the download.php file located also in the folder /download
but it doesnt work, no idea why...
Can you help me?
BTW: the mod_rewrite is enabled.
Thanks.
Your RewriteRule is recursing on itself, and passing download.php as the parameter to itself.
You can try either of the following.
To match only zip/exe:
RewriteRule ^(.*\.(zip|exe))$ download.php?filedownload=$1 [L]
To exclude download.php from matches, place this before your RewriteRule:
RewriteCond %{REQUEST_FILENAME} !download.php

htaccess redirection exceptions

i have the following .htaccess
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?type=Navigation&action=$1
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?type=Navigation&action=$1
Options All -Indexes
which sends everything to index.php in the root folder. But in my structure i have something like
/root
/.htaccess
/index.php
/files
/intranet
i was wondering if there is a RewriteRule exception so that when i type www.mysite.com/intranet it goes to the 'intranet' folder without passing trough the index.php file
thanks!
Yes. I believe you just put a rule before the others with the url you do not want rewritten, and specify that it shouldn't be rewritten using - like so:
RewriteRule ^intranet/.*$ - [L]
RewriteCond %{REQUEST_URI} ^\/intranet\/?
RewriteRule ^([a-zA-Z0-9_-]+)\/?$ index.php?type=Navigation&action=$1 [L]
Will do the trick.

Resources