I am using a shared hosting package with ISAPI Rewrite 3 installed. The problem is that when I try to amend the .htaccess to prevent hotlinking, it doesn't work. Does anyone have experience of ISAPI Rewrite that can shed light on what I'm doing wrong. I put the file is put into the same directory as Default.aspx. The .htaccess is :-
[ISAPI_Rewrite]
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?example.com [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?example.net [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?example.co.uk [NC]
RewriteRule \.(jpg|jpeg|gif)$ - [NC,F,L]
Is the syntax right or something else is the case? As always, your help is appreciated.
Try to add "RewriteBase /", but everything else looks good. It could be smth in your permissions.
Related
I'm trying to understand how to do an URL rewrite using .htaccess file.
The easier-to-digest information that I've found so far is here, and I'm trying to adapt it to my own needs, but unsuccessfully so far.
I have this url:
http://www.example.com/contenido.php?id=19
and I want at the moment to transform it into:
http://www.example.com/contenido/19
I've tried an .htaccess file with:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^contenido/([0-9]+) http://www.example.com/contenido.php?id=$1 [NC]
When I try the clean url I get a "server not found" error.
Any insight will be very appreciated.
Please note that RewriteEngine is ON in my Apache server.
It is not necessary to test if the pages exist, as you rewrite all the contents of the directory.
You can use:
Options +FollowSymLinks
RewriteEngine On
# To externally redirect contenido.php?id=nnn to contenido/nnn
RewriteCond %{THE_REQUEST} \s/+contenido\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /contenido/%1 [R=301,L,NE]
# To internally rewrite contenido/nnn to contenido.php?id=nnn
RewriteRule ^contenido/(\d+/?)?$ contenido.php?id=$1 [NC,L]
After a little help from this tutorial, I've found a solution.
Just in case it helps someone I'll post it here:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^contenido/(\d+)*$ ./contenido.php?id=$1
I would like to do this globally via htaccess rewrite so any www.domain.com/wildcard/ would == www.domain.com/new_segment/wildcard/ but I can't seem to figure this out.
So far, I have the following:
Options +FollowSymLinks -MultiViews
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.domain\.com\/new_segment\/$1" [R=301,L]
For some reason, on the initial page load I get www.domain.com/new_segment/new_segment/ but if I go to www.domain.com/old_segment I would expect www.domain.com/new_segment/old_segment/ but I get www.domain.com/old_segment/ instead.
Any help would be greatly appreciated.
Why would you want that? Anyways, you can do it easier by putting desired files into a directory and a redirection from a file on the /old_segment to /new_segment.
This can be done with:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/new_segment
RewriteRule ^(.*)$ /new_segment/$1 [R=301,L]
It will check to see if the new_segment isn't in the url, if it is then nothing will be redirected. If it isn't then new_segment will be added.
I don't work to much with mod_rewrite so I could easily be just missing something simple. I want to match and replace a specific URL. The URL I want to rewrite would look like this:
http://www.sample.com/hello to http://www.sample.com/index.php?page=hello
My htaccess currently has the following rule in place:
RewriteRule ^(hello)/?$ index.php?page=$1 [L]
However, when I implement the rule and I test it by trying to go to http://www.sample.com/hello or http://www.sample.com/hello/ I always get my 404 error document. ugh I feel like it should be simple and I must be missing something right there. Any help would be fantastic and much appreciated!
Here is my whole file for reference:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?sample\.com/ [NC]
RewriteCond %{REQUEST_URI} !hotlink\.(gif|png) [NC]
RewriteRule ^(.*)\.(gif|jpe?g|png)$ sample.com/hotlink.png[R,NC,L]
RewriteCond %{HTTP_HOST} ^www.sample\.com$ [NC]
RewriteRule ^(.*)$ http://sample.com/$1 [R=301,L]
RewriteRule ^(hello)/?$ index.php?page=$1 [L]
Thanks
Your code works perfeclty fine for me. Maybe you're missing
RewriteEngine On
RewriteRule ^(hello)/?$ page.php?page=$1 [L]
or maybe mod_rewrite is not installed or loaded at all.
Look for
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
in your main apache configuration file httpd.conf
This is one of those times...
I cleared the htaccess file and only used the following couple lines and tested it out:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
#
RewriteRule ^(hello)/?$ page.php?page=$1 [L]
I realized looking at the non custom 404 page that it referenced page.php and not index.php!
Not Found
The requested URL /page.php was not found on this server.
So turns out with all my edits I mistakenly typed page.php in the rule instead of index.php. I corrected it and the rule works. One of those times :)
Thanks all!
My server provider stopped give me possible way to create subdomains in control panel and they say I should solve it with htaccess.
So I have to do something like this. When user types http://asdf.example.com it should get the content of http://example.com/asdf (as the provider changed even the FTP's structure... how kind of them)
I don't find any tutorial for this. Thanks in advance.
More generic version of TerryE's answer. I haven't tested it though!!!
RewriteCond %{HTTP_HOST} !www.example.com
RewriteCond %{HTTP_HOST} (.*).example.com
RewriteCond %1/$0 !^([^/]+)/\1
RewriteRule ^.* %1/$0 [L]
You can do that with mod_rewrite and mod_proxy:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^asdf\.example\.com$
RewriteRule ^ http://example.com/asdf%{REQUEST_URI} [L,P]
You don't need mod proxy. Just do an internal redirect:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} =asdf.example.com
RewriteCond $0 !^asdf/
RewriteRule ^.* asdf/$0 [L]
The rule rewrite the request prefixing asdf/ but only if (1) the host is http:/asdf... and (2) the rewrite hasn't already taken place. You need (2) to prevent iterative loops. $0 is the match string in the rule.
Hope this helps.
I'm trying to tweak some rewrites in an .htaccess file but my regexp/mod_rewrite knowledge is thin!
We have primarydomain.com and secondarydomain.co.uk, and we want to rewrite anything and everything to www.primarydomain.com
At present I'm doing this:
RewriteCond %{HTTP_HOST} ^secondarydomain.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www.secondarydomain.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^primarydomain.com$
RewriteRule (.*)$ http://www.primarydomain.com/$1 [R=301,L]
.. but I'm sure there is a better way?? Is there a rule that would rewrite any host that's not www.primarydomain.com? I tried playing about with the not operator (!) and managed to cause a redirect loop so don't want to do that :/
Any help appreciated! Ben
Ok, Found it myself:
RewriteEngine On
RewriteRule ^(.*)$ http://www.primarydomain.com/ [R=301,L]