mod_rewrite keep rewriting recursilvely - linux

I just want to append the url with home but it is happening in recursion and i get the browser saying too many redirects
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteRule ^test main.html
RewriteRule rest/(.*)$ /home/rest/$1 [R=301]
</IfModule>
so i want
localhost/rest/abc.php
to
localhost/home/rest/abc.php

Remove the slash before home.
RewriteRule ^rest/(.*)$ home/rest/$1 [R=301]

Related

.htaccess / mod_rewrite - adding a temporary mandatory string to the URL doesn't take effect

I want to make my website temporarily accessible only at example.com/unfinished-version.
All URLs like /style/main.css or /public/style/main.css would point to /soon.html, then all URLs like /unfinished-version/style/main.css would point to URLs like /public/style/main.css.
The rule I wrote doesn't work, because /public/style/main.css or /public/some-page still works.
RewriteRule ^(?!unfinished-version).*$ soon.html [L,NC]
URLs like example.com/gibberish show soon.html, so it does partly work.
I'll show you full .htaccess, although I find it unnecessary. (The rules shouldn't interfere with this, plus the L flag should avoid processing of any further rules in the first place.)
.htaccess in the root:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(?!unfinished-version).*$ soon.html [L,NC]
RewriteRule ^unfinished-version(.*)$ public/$1 [L,NC]
</IfModule>
.htaccess in public/:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
</IfModule>
P.S.: Not even RewriteRule ^(.*)$ soon.html [L,NC] works.

How do I redirect in .htaccess file without changing URL

I want to redirect several pages to a PHP page with GET variables. However, when I try to use an .htaccess file, it redirects but changes the URL. I don't want it to change the URL structure when redirecting. How would I do this?
For example, I want to redirect /fly-snooze-cruise to http://example.com/package_listing.php?package-type=fly-snooze-cruise
and /fly-snooze-cruise/orlando-airport to http://example.com/package_listing.php?package-type=fly-snooze-cruise&airport=orlando-airport
Here is my .htaccess code:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^fly-snooze-cruise$ http://www.example.com/package_listing.php?package-type=fly-snooze-cruise [L]
RewriteRule ^fly-snooze-cruise/orlando-airport$ http://www.example.com/package_listing.php?package-type=fly-snooze-cruise&airport=orlando-airport [L]
RewriteRule ^fly-snooze-cruise/orlando-sanford-airport$ http://www.example.com/package_listing.php?package-type=fly-snooze-cruise&airport=orlando-sanford-airport [L]
RewriteRule ^fly-snooze-cruise/melbourne-airport$ http://www.example.com/package_listing.php?package-type=fly-snooze-cruise&airport=melbourne-airport [L]
RewriteRule ^snooze-park-cruise$ http://www.example.com/package_listing.php?package-type=snooze-park-cruise [L]

Redirect url to home page with trailing dots

Google webmaster showing some duplicate url,
They are
www.abc.com/index.php?option=com_toys&view=detail&n_id=148&ite..
www.abc.com/index.php?option=com_toys&view=detail&n_id=156&item..
www.abc.com/index.php?option=com_games&view=detail&vid=170&itemid..
www.abc.com/index.php?option=com_play&view=detail&vid=175&it..
To remove them - i feel the best way is to redirect to home page for any url containing .. at end of url
tried putting this condition, but it does not work too
RewriteRule ^(.*)\.htm$ http://www.abc.com/$1 [R=301,L]
RewriteRule ^(.*)$ http://www.abc.com/$1 [L,R=301]
RewriteRule ^(..*)\.htm$ http://www.abc.com/$1 [R=301,L]
Correct url structure are
www.abc.com/index.php?option=com_toys&view=detail&n_id=148&Itemid=2
www.abc.com/index.php?option=com_toys&view=detail&n_id=156&Itemid=2
www.abc.com/index.php?option=com_games&view=detail&vid=170&Itemid=3
www.abc.com/index.php?option=com_play&view=detail&vid=175&Itemid=4
any suggestions pls ... many thnx
Edit on 13th Sep
Hello Anubhav,
If we have redirect these URL to 404 page then is below command in htaccess correct
RewriteCond %{THE_REQUEST} \?.+?\.\.
RewriteRule ^index\.php$ - [NC,L,R=404]
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 %{THE_REQUEST} \?.+?\.\.
RewriteRule ^ /? [R=301,L,NE]

htaccess Redirect BEFORE RewriteRule?

This is my htaccess:
## Rewrites
<IfModule mod_rewrite.c>
RewriteEngine On
Redirect /stream/ http://twitch.tv/8wayrun
Redirect /stream http://twitch.tv/8wayrun
RewriteCond %{HTTP_HOST} ^(www\.)?8wayrun\.com$
RewriteRule ^(.*)$ http://8wayrun.com/calibur/$1 [R=302,L]
</IfModule>
Basically, I need to to rewrite 8wayrun.com/stream to twitch.tv/8wayrun...
And then I need it to rewrite 8wayrun.com to 8wayrun.com/calibur...
The problem is, its rewriting 8wayrun.com/stream to 8wayrun.com/calibur/stream. How do I fix this?
The Redirect directive is part of mod_alias, and the Rewrite* directives are part of mod_rewrite. When a URI gets processed through the URL/file mapping pipeline, both modules get applied so having one in front of the other doesn't matter, both will get applied in the end.
You're better off sticking with only mod_rewrite and using the L flag to prevent the extra redirects from geting applied.
## Rewrites
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/?stream/? http://twitch.tv/8wayrun [R=302,L]
RewriteCond %{HTTP_HOST} ^(www\.)?8wayrun\.com$
RewriteRule ^(.*)$ http://8wayrun.com/calibur/$1 [R=302,L]
</IfModule>

Rewrite all files from folder to another folder

I want redirect all requested files from folder-a to folder-b. E.g. http://www.yoursite.com/folder-a/index.php to http://www.yoursite.com/folder-b/index.php.
How can I do that? I have verified if mod_rewrite works with this code:
RewriteEngine On
RewriteRule ^google.html$ http://www.google.com/ [R=301]
The structure on the webspace is the following:
.htacces
|-folder-a
|-folder-b
But if I want my folder redirect
RewriteEngine On
RewriteRule ^/folder-a/(.*)$ http://www.yoursite.com/folder-b/$1 [L,R=301]
the redirect doesn't work if I input the following URL:
http://www.yoursite.com/folder-a/
http://www.yoursite.com/folder-a/index.php
The redirect doesn't take place. I stay on the same page ... What I'm doing wrong? I also tried it with this htaccess:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^folder-a(.*)$ http://www.yoursite.com/folder-b$1 [L,R=301]
</IfModule>
If all this works I want to exclude some files. Eg. with this htaccess:
RedirectPermanent /folder-a/info.php /folder-b/new-info.php
Edit:
Now I tried this htaccess
redirectMatch 301 ^/folder-a/ http://www.yoursite.com/folder-b
This works, but I need something which takes the whole path and rewrite it to the new folder.
This for example doesn't work:
RewriteRule ^folder-a/(.*)$ folder-b/$1
Solution
I had an old htacess file in my folder-a so the redirect didn't worked. This is my final htaccess:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^folder-a/excludefile1.php http://www.yoursite.com/folder-b/newnameforfile1.php [L,R=301]
RewriteRule ^folder-a/excludefile2.php http://www.yoursite.com/folder-b/newnameforfile2.php [L,R=301]
RewriteRule ^folder-a/(.*)$ http://www.yoursite.com/folder-b/$1 [L,R=301]
</IfModule>
You probably need to put both rules in place: one for the empty folder and one for other files, and then the catch-all at the bottom. This is not tested:
RewriteEngine On
RewriteRule ^folder-a/info.php http://www.yoursite.com/folder-b/new-info.php [L,R=301]
RewriteRule ^folder-a/ http://www.yoursite.com/folder-b/ [L,R=301]
RewriteRule ^folder-a/(.*)$ http://www.yoursite.com/folder-b/$1 [L,R=301]

Resources