I wanted to redirect all IPs to a specific page except my IP. and I successfully did that, but if that page has some pictures they won't ever load. Tried more than one solution, but nothing works.
How I redirect them using .htacess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule (?:^|/)(css|js|img)/(.+)$ /$1/$2 [NC,QSA,L]
RewriteCond %{HTTP:X-FORWARDED-FOR} !^22\.22\.22\.22
RewriteCond %{REQUEST_URI} !/test.php$ [NC]
RewriteRule .* /test.php [R=302,L]
Images (and CSS and Javascript) don't load, because the first rule causes a rewrite loop. If you just want to serve requests to subfolders css, js or img without modification, you can exit the rule chain by using - as the target, see RewriteRule
- (dash)
A dash indicates that no substitution should be performed (the existing path is passed through untouched). This is used when a flag (see below) needs to be applied without changing the path.
RewriteRule ^(?:css|js|img)/ - [NC,L]
Related
I've tried in in my localhost at it worked fine but after I upload it to my ionos webspace the website index is working but after I click the content it is not directing to anywhere and there is an error message:
Error 404 not foound, Your browser can't find the document corresponding to the URL you typed in.
Here is my .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^news/([0-9a-zA-Z_-]+) news.php?url=$1 [NC,L]
RewriteRule ^seksikateg/([0-9a-zA-Z_-]+) seksikateg.php?kategori=$1 [NC,L]
and i placed he file in the same place as the index.php, news.php, and seksikateg.php
It's possible that MultiViews is enabled at your host and this will break your rules since this will append the .php extension before mod_rewrite processes the request.
However, your directives are also in the wrong order. The generic rewrite to append the .php extension should appear after the other rules.
Your rewrite to append the .php extension is not strictly correct as it could result in a rewrite loop (500 error) under certain circumstances.
Try it like this instead:
# Ensure that MutliViews is disabled
Options -MultiViews
RewriteEngine on
RewriteRule ^news/([0-9a-zA-Z_-]+)$ news.php?url=$1 [NC,L]
RewriteRule ^seksikateg/([0-9a-zA-Z_-]+)$ seksikateg.php?kategori=$1 [NC,L]
# Append ".php" extension on other URLs
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+)$ $1.php [L]
I've also added the end-of-string anchor to the regex of your existing rewrites, otherwise you are potentially matching too much. eg. /news/foo/bar/baz would have also been rewritten to news.php?url=foo - potentially creating duplicate content and opening up your site to abuse.
I would also question the use of the NC flag on these rewrites. If this is required then you potentially have a duplicate content issue.
No need to backslash-escape literal dots in a regex character class and the NC flag is certainly redundant on the last rule.
Essentially, I need to edit the .htaccess file to develop a way to hide folder structure after /file. For example, if I have this URL:
https://www.example.com/file/page/work/assignments.php?
I should only see https://www.example.com/file/assignments.php?
How can this be accomplished?
So far I have tried the following, but it does hide all folders after first:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule !^subfolder/ /subfolder%{REQUEST_URI} [L]
You can do something like the following to "hide" page/work after the /file/ directory and before the last path segment.
This needs to go in the root .htaccess file.
RewriteEngine On
# Redirect "/folder/page/work/<file>" to "/folder/<file>"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(folder)/page/work/([^/]+)$ /$1/$2 [R=302,L]
# Rewrite "/folder/<file>" back to "/folder/page/work/<file>" (if it exists)
RewriteCond %{DOCUMENT_ROOT}/$1/page/work/$2 -f
RewriteRule ^(folder)/([^/]+)$ $1/page/work/$2 [L]
The will externally redirect a request for /folder/page/work/assignments.php?something to /folder/assignments.php?something. And internally rewrite the request back again.
NB: You should already be linking to /folder/<file> internally. The initial redirect to remove page/work is just for SEO if you are changing an existing URL structure. Otherwise, this is not strictly necessary.
A look at your existing rule...
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule !^subfolder/ /subfolder%{REQUEST_URI} [L]
This would seem to be unrelated to what you are trying to achieve. It would internally rewrite a request for /<anything> to /subfolder/<anything>, essentially "hiding" /subfolder.
There is no need to check the requested Host header unless you are accepting requests to multiple domains and this is specific to just one of those domains. (You've made no mention of this in your question.)
I've a domain that contains a subfolder with the web app structure. I added a .htaccess on my root domain to point the public folder on my subfolder web app. It works fine, but when I type www.example.com the browser URL changes to www.example.com/subfolder/public, but I would like that it doesn't change.
This is my .htaccess:
RewriteEngine On
RewriteRule ^.*$ subfolder/public [NC,L]
EDIT
This first .htaccess is used to redirect on subfolder/public, where there is an other .htaccess that makes all the works.
Here the code of the second .htaccess located on www.example.com/subfolder/public/:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Sorry, just realised what is happening. It has nothing to do with the second .htaccess file in the subdirectory, as mentioned in comments.
RewriteRule ^.*$ subfolder/public [NC,L]
Since public is a physical directory on the file system, you need to include a trailing slash when internally rewriting to that directory. Otherwise, mod_dir is going to try to "fix" the URL by appending a slash - that is where the external redirect is coming from. (mod_dir implicitly triggers an external redirect from subfolder/public to subfolder/public/.)
So, try the following instead in your root .htaccess file:
RewriteRule .* subfolder/public/ [L]
The important thing is the trailing slash. The anchors (^ and $) on the RewriteRule pattern are not required, since you are matching everything. And the NC flag is also not required for the same reason.
As always, make sure the browser cache is clear before testing.
UPDATE#1: The single directive above rewrites everything, including static resources, to the directory subfolder/public/ which then relies on the second .htaccess file in the subdirectory to correctly route the request. In order to allow static resources to be rewritten correctly (represented in the HTML as root-relative URL-paths, of the form "/js/myjs.js") then you will need additional directives in order to rewrite these.
For example, to specifically rewrite all .js and .css files to the real location in /subfolder/public/...
# Rewrite static resources
RewriteRule (.+\.(?:js|css))$ subfolder/public/$1 [L]
# Rewrite everything else to the "public" directory
RewriteRule .* subfolder/public/ [L]
UPDATE#2: To make the above more general, and to rewrite any static resource (images, PDFs, .txt, etc...) we can check for the existence of the file before rewriting, something like:
# Rewrite static resources
RewriteCond %{DOCUMENT_ROOT}/subfolder/public/$1 -f
RewriteRule (.+) subfolder/public/$1 [L]
# Rewrite everything else to the "public" directory
RewriteRule .* subfolder/public/ [L]
This will mean that if any .css does not exist it will be passed through to subfolder/public/.
I'm having trouble making sense of this .htaccess file I've inherited at my new job. I'm a graphic designer / marketer learning web development. Our site has a bunch of duplication errors and I want to rename our links and have them 301 redirected.
This is the first part of the .htaccess file:
RewriteEngine Off
RewriteCond %{HTTP_HOST} !^blog.asseenontvhot10.com$ [NC]
RewriteCond %{REQUEST_URI} !/sitemap\.xml
AddType text/html .shtml, .html, .htm
AddHandler server-parsed .shtml, .html, .htm
AddHandler application/x-httpd-php5 .html .htm .shtml
#Options FollowSymLinks Includes -Indexes
From what I understand the first part links our blog to the main site, and I'm not sure what the next line does.
The addtype line and the ones that follow enable .htaccess to read/write those filetypes?
Here is the part that I believe is relevant and what I need to change:
RewriteEngine on
rewritecond %{http_host} ^[^.]+\.[^.]+$ [nc]
rewriterule ^(.*)$ http://www.%{http_host}/$1 [r=301,nc]
RewriteRule default.html /home.html
RewriteRule contact\.html /cgi-bin/commerce.cgi?display=contact
RewriteRule about\.html /cgi-bin/commerce.cgi?display=about
RewriteRule register\.html /cgi-bin/commerce.cgi?register=action
RewriteRule products\.html /cgi-bin/commerce.cgi?listcategories
RewriteRule parent_category/(.*)/(.*)/ /cgi-bin/commerce.cgi?listcategories=action&parent=$1
RewriteRule category/(.*)/(.*)/(.*)\.html /cgi-bin/commerce.cgi?search=action&category=$1&page=$3
RewriteRule category/(.*)/(.*)/ /cgi-bin/commerce.cgi?search=action&category=$1
RewriteRule category/(.*).html /cgi-bin/commerce.cgi?search=action&page=$1
RewriteRule product/(.*)/(.*)/ /cgi-bin/commerce.cgi?preadd=action&key=$1
RewriteRule basket\.html /cgi-bin/commerce.cgi?display=basket
RewriteRule log-in\.html /cgi-bin/commerce.cgi?login
RewriteRule home\.html /cgi-bin/commerce.cgi?display=home
RewriteRule search\.html /cgi-bin/commerce.cgi?display=search
RewriteRule all-items\.html /cgi-bin/commerce.cgi?search
RewriteRule update_user\.html /cgi-bin/commerce.cgi?displayuser=action
RewriteRule order_history\.html /cgi-bin/commerce.cgi?orderhistory
RewriteRule logout\.html /cgi-bin/commerce.cgi?logout
RewriteRule specials\.html /cgi-bin/commerce.cgi?search=action&keywords=nav_specials
I know the first part forces www. and does a 301 redirect, even though I haven't seen this way of accomplishing it on any guide I've read. The first thing I really need to change is making the home page 301 redirect from:
http://www.asseenontvhot10.com/cgi-bin/commerce.cgi?display=home
to
http://www.asseenontvhot10.com/
So far as I can tell the RewriteRule makes this an option but dosn't force it. Will adding [r=301] to the end of the rewriterule work?
I've also read that there is code you need to add for .cgi to work as .php does but I'm totally lost there.
Update:
I tried adding this line to the bottom -
Redirect 301 /cgi-bin/commerce.cgi?display=home /
and I got a 500 error.
2nd Update:
I added this code:
RewriteRule about\.html /cgi-bin/commerce.cgi?display=about
RewriteCond %{QUERY_STRING} ^display=about$
RewriteRule ^cgi-bin/commerce\.cgi$ /about [R=301]
And it 404'd. When I replaced the .htaccess file with the back up and the about page still 404's... Now I'm really at a loss and don't know how to revert the site. I don't even know where the redirect would be stored outside of the htaccess file once it's reverted.
3rd Update: (making progress)
I added this code:
RewriteRule about\.html /cgi-bin/commerce.cgi?display=about
RewriteCond %{QUERY_STRING} ^display=about$
RewriteRule ^cgi-bin/commerce\.cgi$ /about\.html? [R=301]
and I got a redirect loop. It was finally redirecting to about.html which I thought the first rule renamed /cgi-bin/commerce.cgi?display=about to, but it redirected to it over and over again.
The Redirect directive in Apache requires that the redirect destination be a fully qualified URL with a scheme and a URL. So this would work:
Redirect 301 /cgi-bin/commerce.cgi http://www.yourdomain.com/
However, I don't believe that Redirect can check the query string, so in order to only apply this redirect if the query string is "display=home" you will probably have to use mod_rewrite like this:
RewriteCond %{QUERY_STRING} ^display=home$
RewriteRule ^cgi-bin/commerce\.cgi$ / [R=301]
The RewriteCond checks that the query string exactly equals "display=home" and the RewriteRule will only trigger if this condition is met. And in the RewriteRule the [R=301] flag instructs mod_rewrite to redirect the visitor's web browser to the target path, / in this case.
By the way, looking at the existing conditions in your .htaccess file, I can't see how your users are seeing the /cgi-bin/commerce.cgi path in their web browser. All of those RewriteRule directives are for silent rewrites. This means that the user will continue to see the "friendly" URL in their web browser, but in the background Apache will actually find the file located at the /cgi-bin/commerce.cgi path and return the content or result of that file to the user. So your users should already be seeing the friendly version of the URL. If what you are trying to do instead is silently rewrite a request for "/" (site root) so that Apache silently rewrites the path and actually fetches the content in the real file /cgi-bin/commerce.cgi?display=home then you probably want this instead:
RewriteRule ^$ /cgi-bin/commerce.cgi?display=home
This will only match a request for the root directory (with no filename specified) and will silently fetch and return the content which results from calling the commerce.cgi file with a "display=home" parameter.
I'm setting up URL rewrite rules for an application I'm developing so that I can use nice clean URLs. I want the URLs to look like http://app.com/page/agency/ and to be equivalent to http://app.com/index.php?p=page&agency=agency. The agency selector is optional, so I want the URLs to redirect, even if the agency is not present. I have created the following mod_rewrite rules for this purpose:
RewriteRule ^/?([a-z]+)/$ /index.php?p=$1 [PT]
RewriteRule ^/?([a-z]+)/([a-z]+)/$ /index.php?p=$1&agency=$2 [PT]
This is working fine for redirecting the pages. However, it seems to me that my javascript files are being re-loaded with each page, as if the browser thinks that it's in a different directory and needs to re-load the JS files. The JS files are linked using a hard-coded URL, such as http://app.com/scripts/dostuff.js.
Is it possible that the browser is reloading the javascript files each time? If so, have I done something wrong?
Try this code:
RewriteEngine On
# skip rewrite rules below it is a valid file or a valid directory
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# write single path
RewriteRule ^([a-z]+)/?$ /index.php?p=$1 [L,QSA]
# write 2 paths
RewriteRule ^([a-z]+)/([a-z]+)/?$ /index.php?p=$1&agency=$2 [L,QSA]