My website : theexample.com
I modified my .htaccess so that if visitors visits an unkown url it redirects directly to index.php with :
FallbackResource index.php
The problem is that it works only if there is one element after "theexample.com".
for example :
"theexample.com/shudhusdfuisdfhisdfh": works and redirects to
"theexample.com/sh/fuisd/fhisdfh": doesn't work and doesn't
redirect to index.php.
You must use absolute path of fallback resource not a relative one:
FallbackResource /index.php
Otherwise FallbackResource index.php will try to load index.php in the provided sub-path e.g. theexample.com/sh/fuisd/fhisdfh will try to load theexample.com/sh/fuisd/index.php which will cause failure as that path doesn't exist resulting in Internal Server Error.
This is rewrite. Redirect only non-files links =)
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ index.php
Example: url http://example.com/asdjivuhr/34tv3t will redirext to index.php
BUT if you have files(css,js) http://example.com/script.js will stay untooched if file exists!
Related
I'm trying to direct all traffic to the homepage only to a php script called go.php that gets a variable from the URL.
If someone visits domain.com/username go.php gets the username, looks up their information, saves the information to a session and then redirects to index.php and displays a modified version of the homepage (same domain) that has the retrieved information. Everything works except the mod rewrite part.
I tried the following and am not sure what I am doing wrong:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^/$ go.php?id=$1 [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
My logic was that if a request is to index.php it should be allowed, to prevent looping.
If the request is to the homepage it will go to go.php?id=username and the that script will redirect to index.php and trigger the prior mod rewrite rule to prevent looping.
Otherwise, it will do the regular redirect to index.php if the directory or filename doesn't exist.
Any thoughts on how to fix this?
I think this is what you mean:
RewriteRule ^(.*)$ go.php?id=$1 [QSA]
Explanation:
I'm looking at the ^/$ in the regular expression in the go.php rewrite rule. I've tested that and it appears to be an impossible match in that situation. One might be wanting to capture requests for root. But the forward slash is not passed to this portion of the RewriteRule for root. so a call for root (only) is ^$. And there's also no capturing parenthesis to feed the $1 you have appended to go.php?id=$1.
If none of my RewriteRule [L] matches, I want to redirect to a nice url /you/shall/not/pass, but show contents of /index.html.
This is what I am doing now (this is the very last rule in the file):
RewriteCond %{REQUEST_FILENAME} !-f # is not file
RewriteCond %{REQUEST_FILENAME} !-d # is not directory
RewriteCond %{ENV:REDIRECT_STATUS} !=200 # wasn't already redirected
RewriteRule .* index.html
It works fine, but keeps whatever garbage was written in the URL. I want it to be changed.
Doing this didn't work
#RewriteCond same as above
RewriteRule .* /you/shall/not/pass [R]
RewriteRule ^/you/shall/not/pass index.html
I apparentely don't understand how [R] works, whether it continues forwarding the changed url to other RewriteRules or not and what page it redirects to when the end of file is reached.
You can use ErrorDocument 404 with a rewrite rule for this:
Options +FollowSymLinks
ErrorDocument 404 http://domain.com/you/shall/not/pass
Then create a symbolic link like this:
/public_html/you/shall/not/pass -> /public_html/index.html
Replace /public_html/ with your DocumentRoot path.
So, after countless hours I made it work, somehow, I guess...
I am too tired to investigate, but I will get back later and edit if it is wrong.
ErrorDocument 404 http://domain.com/you/shall/not/pass # redirects if document does not exist
RewriteEngine on
RewriteRule ^$ /you/shall/not/pass [L,R] # redirects if request URI is empty, skips rest
RewriteRule ^you/shall/not/pass$ /index.html [L] # puts different resource to matched url
It's kinda weird that the solution is that short.. How come I didn't try that already?
I've been looking for a precise answer to this question for awhile but couldn't find it.
I've launched a one page website in place of an old website with many pages. Now, I want to redirect everything to www.domain.com. The page uses index.html as the homepage, but I don't want to redirect to that, I just want to redirect to the www.domain.com root.
I tried using:
RewriteRule ^.+$ / [R=302,NC,L]
But that just broke my stylesheet and didn't redirect anything. Other solutions I've seen have redirected to the index.html but I want to redirect to the / root domain.
Also, I want to be sure to redirect all non-www pages to www pages too. Can someone please help me out?
Much appreciated
You have most likely something like this in your httpd.conf:
<IfModule dir_module>
DirectoryIndex index.php index.php3 index.html index.htm
</IfModule>
If you request a folder (and http://example.com/ is a request to a folder), it will try index.php first, then index.php3, etc, etc, and the first one that exists it will shown. You'll have to delete that from your httpd.conf if you want every request to end up as http://example.com. It'll show a directory view of your www-root folder unless that has been disabled.
Try:
# Any direct request for html/php pages
RewriteCond %{THE_REQUEST} /[^\?\ ]+\.(html?|php.) [NC]
RewriteRule ^ / [L,R=301]
That won't affect images, or style sheets, etc. Note that this matches against the %{THE_REQUEST} variable because internally the %{REQUEST_URI} gets converted to /index.html so you can't match against that.
If you want non-existent requests (which would normally result in a 404) to be redirected as well:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ / [L,R=301]
To force a "www" use:
RewiteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
I had a website which url is http://www.testingmyweb.comli.com
In my website a had a folder TCWEB. My website is in this folder.
I want to map http://testingmyweb.comli.com/TCWEB/home.html to http://testingmyweb.comli.com/
What can I changes do in .htaccess file?
Based on your comment, your htaccess file looks like this:
RewriteBase /
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Redirect /TCWEB/home.html testingmyweb.comli.com
The rule that you have appends .html to requests that are for html files (but don't have the extension). The redirect that you have underneath that needs to be removed. In order for you to rewrite / to /TCWEB/home.html, that mod_alias directive will cause a loop. Replace it with these rules:
RewriteRule ^$ /TCWEB/home.html [L]
RewriteCond %{THE_REQUEST} /TCWEB/home.html
RewriteRule ^ / [L,R=301]
The first rule makes it so requests for / maps to /TCWEB/home.html, but the URL in the browser's address bar remains http://www.testingmyweb.comli.com. The second rule does what the Redirect does, except it checks that the request was actually made for /TCWEB/home.html, and not an internally rewritten URI. So when someone goes directly to /TCWEB/home.html, they get redirected to /, then the first rule gets applied.
I'm doing some work for a friend on their website, and while we are working on the new site I would like to set up their .htaccess file to redirect users with the following rules:
Redirect any requests to / or /index.html to a subdirectory (e.g. both http://example.com/ and http://example.com/index.html should redirect to http://example.com/legacy/index.html)
Allow direct requests to index.php to pass through without redirect (e.g. http://example.com/index.php)
If possible, requests to a second subdirectory get redirected to index.php (e.g. http://example.com/beta/ redirects to http://example.com/index.php)
This is probably a simple request, but I have no experience with the rules language that .htaccess uses.
Thanks in advance!!
Redirect any requests to / or /index.html to a subdirectory (e.g. both http://example.com/ and http://example.com/index.html should redirect to http://example.com/legacy/index.html)
RewriteEngine On
RewriteRule ^(index\.html)?$ /legacy/index.html [L]
Allow direct requests to index.php to pass through without redirect (e.g. http://example.com/index.php)
RewriteRule ^index\.php$ - [L]
If possible, requests to a second subdirectory get redirected to index.php (e.g. http://example.com/beta/ redirects to http://example.com/index.php)
RewriteRule ^beta/?$ /index.php [L]
These would all go in the htaccess file in your document root. If you actually wanted to redirect the browser so that the URL in the address bar changes, include a R=301 in the square brackets: [L,R=301]. If when you say "requests to a second subdirectory get redirected to index.php" meaning "any subdirectory", then the rule needs to be changed to:
RewriteRule ^([^/]+)/?$ /index.php [L]
i kind of did the reverse with codeigniter ...
RewriteEngine On
RewriteBase /myproject
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
$config['base_url'] = 'http://localhost/myproject/';
$config['index_page'] = '';
hope this helps