.htaccess seems to continue after Last flag - .htaccess

In the small portfolio website I am making I want to have clean urls. For this I use a .htaccess file. At the moment my website runs at 127.0.0.1/website. My .htaccess looks like this:
RewriteEngine On
RewriteBase /website
RewriteRule ^about$ index.php?page=0 [L,NC]
RewriteRule ^portfolio$ index.php?page=1 [L,NC]
RewriteRule ^cv$ index.php?page=2 [L,NC]
RewriteRule ^contact$ index.php?page=3 [L,NC]
RewriteRule ^.*$ about [L,R]
If I remove the last line, everything works fine. I can go to 127.0.0.1/website/about, and the server shows me 127.0.0.1/website/index.php?page=0, while the url does not change. However, I want every other url to be redirected to 127.0.0.1/website/about, and I tried to do that with the last line. However, if I now try to go to 127.0.0.1/website/anything, where anything is any string, it redirects me to 127.0.0.1/website/about?page=0. In addition to this, I would expect 127.0.0.1/website/about to be handles fine, because of the Last flag at the first rewrite rule, but this redirects me to the same bad url.
I am clueless of why this does not work, and confused, because to me it seems like it is skipping the Last flag. I hope someone can point out what I am doing wrong. Thanks in advance.

last rule rewrites everything so pattern must be more specific
RewriteRule !^index\.php$ about [L,R]

This might work:
RewriteEngine On
RewriteBase /website
RewriteRule ^about$ index.php?page=0 [L,NC]
RewriteRule ^portfolio$ index.php?page=1 [L,NC]
RewriteRule ^cv$ index.php?page=2 [L,NC]
RewriteRule ^contact$ index.php?page=3 [L,NC]
RewriteCond %{REQUEST_URI} !^/cv [NC]
RewriteCond %{REQUEST_URI} !^/about [NC]
RewriteCond %{REQUEST_URI} !^/contact [NC]
RewriteCond %{REQUEST_URI} !^/portfolio [NC]
RewriteRule ^.*$ /about [R,L]
It will redirect everything that isn't /cv, /about, /contact or /portfolio to /about.

Related

Why doesn't this htaccess RewriteRule "stick"?

I'm trying to redirect http://vacation.website.com/category/spa-vacations/ to http://website.com/vacations/spa-vacations/
RewriteEngine On
RewriteRule ^category/(.*)$ $1
RewriteCond %{HTTP_HOST} ^vacation\.website\.com [NC]
RewriteRule ^(.*) https://website.com/vacations/$1 [L,R=301]
With this htaccess, the first rule seems to be applied and removes "category" from the URL - then it's like it never happened and I'm left with http://website.com/vacations/category/spa-vacations/
Why doesn't the first change to the URL stick and is it possible to make it do so?
To redirect
http://vacation.website.com/category/spa-vacations/
to
http://website.com/vacations/spa-vacations/
Try this :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?vacation\.website\.com [NC]
RewriteRule ^category/(.*)$ https://website.com/vacations/$1 [L,R=301]
Note: clear browser cache then test.

RewriteRule is creating new URLs but not redirecting them from the dynamic ones

My website http://www.musicea.com.au has dynamic URLs. I have used .htaccess to rewrite the static URLs. When I copy the rewritten static urls and paste directly in the browser they open. But they are not redirecting automatically. My code is as given below.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page-([0-9]+)\$ content.php?pageid=$1 [L]
RewriteRule ^$ content.php?pageid=home
RewriteRule ^about$ content.php?pageid=1341369117 [L,NC]
RewriteRule ^music-lessons$ content.php?pageid=1344314997 [L,NC]
RewriteRule ^piano-lessons$ content.php? pageid=1347926925&getsubnavid=1344314997 [L,NC]
RewriteRule ^keyboard-lessons$ content.php?pageid=1347927089&getsubnavid=1344314997 [L,NC]
RewriteRule ^guitar-lessons$ content.php?pageid=1347927135&getsubnavid=1344314997 [L,NC]
RewriteRule ^violin-lessons$ content.php?pageid=1347927198&getsubnavid=1344314997 [L,NC]
RewriteRule ^content.php?pageid=home$ http://www.musicea.com.au/ [L,NC]
I think the URLs are rewritten properly but there's problem in redirecting.
Please suggest me.
Here is an example of what I meant in the comments. You only have a rule to internally rewrite to your PHP file, but you don't have a rule to redirect the use of the php file directly. So I'll show you an example of how to do that. I did one rule so that you can see. But because all your URI's don't have any mapping to your dynamic URL you will have to do a similar rule for each one of you rules.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page-([0-9]+)\$ content.php?pageid=$1 [L]
RewriteRule ^$ content.php?pageid=home
RewriteRule ^about$ content.php?pageid=1341369117 [L,NC]
#redirect direct request for content.php for music lesson
RewriteCond %{THE_REQUEST} [A-Z]{3,}\ /content\.php\?pageid=1344314997
RewriteRule ^ /music-lessons? [R=301,L]
RewriteRule ^music-lessons$ content.php?pageid=1344314997 [L,NC]
RewriteRule ^piano-lessons$ content.php? pageid=1347926925&getsubnavid=1344314997 [L,NC]
RewriteRule ^keyboard-lessons$ content.php?pageid=1347927089&getsubnavid=1344314997 [L,NC]
RewriteRule ^guitar-lessons$ content.php?pageid=1347927135&getsubnavid=1344314997 [L,NC]
RewriteRule ^violin-lessons$ content.php?pageid=1347927198&getsubnavid=1344314997 [L,NC]
Let me know if this helped.

WSGI .htaccess with addon domain

I have a subfolder in which I am running a wsgi app. I also have an addon domain pointing to this subfolder. I have trouble setting up the .htaccess file in the subfolder.
My current file structure:
public_html/
.htaccess
irrelevant index.html
some other irrelevant stuff
sub_folder/
.htaccess
app.wsgi
I have a maindomain.com pointing to /public_html/, and an addondomain.com pointing to /sub_folder/.
Currently the .htaccess file in the root folder is empty, the one in sub_folder is:
RewriteEngine On
RewriteBase /
RewriteRule ^(app\.wsgi/.*)$ - [L]
RewriteRule ^(.*)$ app.wsgi/$1 [QSA,L]
This works if I go to addondomain.com, but gives me a 404 when I go to maindomain.com/sub_folder. The log shows that it looks for /public_html/app.wsgi then, instead of /public_html/sub_folder/app.wsgi. How can I rewrite the .htaccess to make it work in both scenarios?
This should work with add-on domain as well.
RewriteEngine On
RewriteBase /
RewriteRule ^(app\.wsgi/.*)$ - [L]
RewriteCond %{HTTP_HOST} ^maindomain.com$ [NC]
RewriteRule ^sub_folder/(.*)$ sub_folder/app.wsgi/$1 [NC,QSA,L]
RewriteCond %{HTTP_HOST} ^addondomain.com$ [NC]
RewriteRule ^(sub_folder/)?(.*)$ app.wsgi/$1 [NC,QSA,L]
EDIT :
The rules above need to be tweaked since OP's .htaccess is already within /sub_folder. The ones above would still work if anyone wants their root /.htaccess to be in the driving seat.
RewriteEngine On
RewriteBase /
RewriteRule ^(app\.wsgi/.*)$ - [L]
RewriteCond %{HTTP_HOST} ^maindomain.com$ [NC]
RewriteRule ^(.*)$ sub_folder/app.wsgi/$1 [NC,QSA,L]
RewriteCond %{HTTP_HOST} ^addondomain.com$ [NC]
RewriteRule ^(.*)$ app.wsgi/$1 [NC,QSA,L]
I made it work, sort of...
RewriteEngine On
RewriteBase /
RewriteRule ^(app\.wsgi/.*)$ - [L]
RewriteCond %{REQUEST_URI} ^/sub_folder [NC]
RewriteRule ^(.*)$ sub_folder/app.wsgi/$1 [QSA,L]
RewriteCond %{REQUEST_URI} !^/sub_folder[NC]
RewriteRule ^(.*)$ app.wsgi/$1 [QSA,L]
#Ravi's answer works with addondomain.com, but not with maindomain.com/sub_folder, because in that case the index is showed, because it is not redirected to app.wsgi.
To solve this, I added a second RewriteCond and RewriteRule. The problem now is, it doesn't work when you go to addondomain.com/sub_folder (although that shouldn't happen). So if there is a better answer that also handles that properly, please enlighten me. Otherwise I'll just hope that that situation doesn't happen.
Edit:
This works:
RewriteEngine On
RewriteBase /
RewriteRule ^(app\.wsgi/.*)$ - [L]
RewriteCond %{HTTP_HOST} ^maindomain.com$ [NC]
RewriteRule ^(.*)$ sub_folder/app.wsgi/$1 [NC,QSA,L]
RewriteCond %{HTTP_HOST} ^addondomain.com$ [NC]
RewriteRule ^(.*)$ app.wsgi/$1 [NC,QSA,L]
Ravi's exact code didn't work, because this .htaccess is in the sub_folder, so the ^(.*)$ part is only the stuff after sub_folder/, somehow. But modified like this it works. Thanks for the help.

redirect all urls with "videos.php" to base

I'm hoping someone can assist me with a mod_rewrite rule to redirect dynamic urls pointing to "videos.php" on my server to the base url.
For example, I need to redirect 'website.com/1/music/various/videos.php?=1234'
to 'website.com/videos.php?=1234'
edit: I am looking for a dynamic solution. If a url is pointed to videos.php at any time, I need to do a 301 redirect to home directory. Ie if /1/home/music/videos.php?=1234 redirect to /videos.php?=1234, or /music/playlist/1234/videos.php?1432 to /videos.php?1432.
Create a .htaccess file and insert these lines:
RewriteEngine On
# Page moved permanently:
RewriteRule ^videos\.php\?\=([0-9]+)\.html$ /1/music/various/videos.php?=$1 [R=301,L]
When testing, leave out the R=301, part, or you'll only see cache contents for a long, long time. Add it when sure its working fine. Or use 302, which means temporarily.
Please check this:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/1/music/various/videos.php$
RewriteCond %{QUERY_STRING} (.+)$
RewriteRule ^(.*) /videos.php [R,QSA]
RewriteCond %{REQUEST_URI} ^/videos.php$
RewriteCond %{QUERY_STRING} !(.+)$
RewriteRule ^(.*) http://%{HTTP_HOST}/ [R=301]
If it's not work, then try this:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/1/music/various/videos.php$
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^(.*) /videos.php%1 [R]
RewriteCond %{REQUEST_URI} ^/videos.php$
RewriteCond %{QUERY_STRING} !(.+)$
RewriteRule ^(.*) http://%{HTTP_HOST}/ [R=301]

How can I use .htaccess rewrite to redirect root URL to subdirectory?

Trying to get
www.example.com
to go directly to
www.example.com/store
I have tried multiple bits of code and none work.
What I've tried:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.+)\www.example\.com$
RewriteRule ^/(.*)$ /samle/%1/$1 [L]
What am I doing wrong?
You can use a rewrite rule that uses ^$ to represent the root and rewrite that to your /store directory, like this:
RewriteEngine On
RewriteRule ^$ /store [L]
I was surprised that nobody mentioned this:
RedirectMatch ^/$ /store/
Basically, it redirects the root and only the root URL.
The answer originated from this link
Try this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^$ store [L]
If you want an external redirect (which cause the visiting browser to show the redirected URL), set the R flag there as well:
RewriteRule ^$ /store [L,R=301]
Here is what I used to redirect to a subdirectory. This did it invisibly and still allows through requests that match an existing file or whatever.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?site.com$
RewriteCond %{REQUEST_URI} !^/subdir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdir/$1
RewriteCond %{HTTP_HOST} ^(www.)?site.com$
RewriteRule ^(/)?$ subdir/index.php [L]
Change out site.com and subdir with your values.
To set an invisible redirect from root to subfolder, You can use the following RewriteRule in /root/.htaccess :
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/subfolder
RewriteRule ^(.*)$ /subfolder/$1 [NC,L]
The rule above will internally redirect the browser from :
http://example.com/
to
http://example.com/subfolder
And
http://example.com/foo
to
http://example.com/subfolder/foo
while the browser will stay on the root folder.
Another alternative if you want to rewrite the URL and hide the original URL:
RewriteEngine on
RewriteRule ^(.*)$ /store/$1 [L]
With this, if you for example type http://www.example.com/product.php?id=4, it will transparently open the file at http://www.example.com/store/product.php?id=4 but without showing to the user the full url.
This seemed the simplest solution:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) http://www.example.com/store [R=301,L]
I was getting redirect loops with some of the other solutions.
Most of the above solutions are correct but they are all missing the transparency of the redirection.
In my case, when visiting www.example.com I wanted to get redirected to the subdirectory /store but without updating the URL to www.example.com/store. (all I want is to get the page code form that directory). If that is your case the solution below works perfectly.
RewriteEngine on
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /store/$1 [L]
source: http://wiki.dreamhost.com/Transparently_redirect_your_root_directory_to_a_subdirectory
I don't understand your question...
If you want to redirect every request to a subfolder:
RewriteRule ^(.*)$ shop/$1 [L,QSA]
http://www.example.com/* -> wwwroot/store/*
If you want to redirect to a subfolder which has the domain name
RewriteCond %{HTTP_HOST} ([^\.]+\.[^\.]+)$
RewriteRule ^(.*)$ %1/$1 [L,QSA]
http://www.example.com/* -> wwwroot/example.com/*
I have found that in order to avoid circular redirection, it is important to limit the scope of redirection to root directory.
I would have used:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) http://www.example.com/store [R=301,L]
Formerly I use the following code which is work correctly to redirect root URL of each of my domains/subdomains to their correspondence subdirectories which are named exactly as the sub/domain it self as below:
RewriteCond %{HTTP_HOST} ^sub1.domain1.com
RewriteCond %{REQUEST_URI} !subs/sub1.domain1.com/
RewriteRule ^(.*)$ subs/%{HTTP_HOST}/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sub2.domain1.com
RewriteCond %{REQUEST_URI} !subs/sub1.domain2.com/
RewriteRule ^(.*)$ subs/%{HTTP_HOST}/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sub1.domain2.com
RewriteCond %{REQUEST_URI} !subs/sub1.domain2.com/
RewriteRule ^(.*)$ subs/%{HTTP_HOST}/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sub2.domain2.com
RewriteCond %{REQUEST_URI} !subs/sub2.domain2.com/
RewriteRule ^(.*)$ subs/%{HTTP_HOST}/$1 [L,QSA]
However when I want to add another subs or domains then it will need to be added in the above code. It should be much more convenient to simplify it to work like wildcard (*) as below:
RewriteCond %{HTTP_HOST} ^sub
RewriteCond %{REQUEST_URI} !/subs/
RewriteRule ^(.*)$ subs/%{HTTP_HOST}/$1 [L,QSA]
So whenever another subdomains/domains is added as long as the subdomain name has a prefix of sub (like: sub3.domain1.com, sub1.domain3.com etc.) the code will remain valid.
Two ways out of possible solutions to achieve this are:
1. Create a .htaccess file in root folder as under (just replace example.com and my_dir with your corresponding values):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/my_dir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /my_dir/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ my_dir/index.php [L]
</IfModule>
Use RedirectMatch to only redirect the root URL “/” to another folder or URL,
RedirectMatch ^/$ http://www.example.com/my_dir
I think the main problems with the code you posted are:
the first line matches on a host beginning with strictly sample.com, so www.sample.com doesn't match.
the second line wants at least one character, followed by www.sample.com which also doesn't match (why did you escape the first w?)
none of the included rules redirect to the url you specified in your goal (plus, sample is misspelled as samle, but that's irrelevant).
For reference, here's the code you currently have:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sample.com$
RewriteRule (.*) http://www.sample.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.+)\www.sample\.com$
RewriteRule ^/(.*)$ /samle/%1/$1 [L]
One can use Redirect too for this purpose
Redirect 301 / www.example.com/store
Or Alias for mapping
Alias / /store
Edit: mod_alias is only applicable in httpd.conf.
Refrences
http://httpd.apache.org/docs/2.2/mod/mod_alias.html
https://httpd.apache.org/docs/trunk/rewrite/avoid.html
A little googling, gives me these results:
RewriteEngine On RewriteBase
/ RewriteRule ^index.(.*)?$
http://domain.com/subfolder/
[r=301]
This will redirect any attempt to
access a file named index.something to
your subfolder, whether the file
exists or not.
Or try this:
RewriteCond %{HTTP_HOST}
!^www.sample.com$ [NC]
RewriteRule ^(.*)$
%{HTTP_HOST}/samlse/$1 [R=301,L]
I haven't done much redirect in the .htaccess file, so I'm not sure if this will work.
try to use below lines in htaccess
Note: you may need to check what is the name of the default.html
default.html is the file that load by default in the root folder.
RewriteEngine
Redirect /default.html http://example.com/store/
you just add this code into your .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /folder/index.php [L]
</IfModule>
This will try the subdir if the file doesn't exist in the root. Needed this as I moved a basic .html website that expects to be ran at the root level and pushed it to a subdir. Only works if all files are flat (no .htaccess trickery in the subdir possible). Useful for linked things like css and js files.
# Internal Redirect to subdir if file is found there.
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-s
RewriteCond %{DOCUMENT_ROOT}/subdir/%{REQUEST_URI} -s
RewriteRule ^(.*)$ /subdir/$1 [L]
I'll answer the original question not by pointing out another possible syntax (there are many amongst the other answers) but by pointing out something I have once had to deal with, that took me a while to figure out:
What am I doing wrong?
There is a possibility that %{HTTP_HOST} is not being populated properly, or at all. Although, I've only seen that occur in only one machine on a shared host, with some custom patched apache 2.2, it's a possibility nonetheless.

Resources