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?
Related
I am trying to achieve a simple redirect - from /news to /insights
I have the following in my .htaccess file:
redirect 301 /news /insights
<IfModule mod_rewrite.c>
RewriteEngine On
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]
</IfModule>
Whenever I've used the redirect directive before, any matched URLs would be redirected and no further rewrites in the file would be processed. That is to say, going to /news would send you to /insights, and the rewrite to index.php would not be processed.
However, with this current setup, going to /news sends me to /insights?p=news, so for some reason the rewrite to index.php is still being processed.
Furthermore, if I comment out the index.php rewrite, then I get sent to /insights as expected.
This isn't how I've usually experienced this working so am unsure why it's doing this.
I have also tried the following:
RewriteEngine On
RewriteRule "^/news" "/insights" [R=301,L]
This simply results in a 404 instead of redirecting, which I also do not understand.
I am aware I could do the following:
RewriteEngine On
RewriteCond %{REQUEST_URI} "^/news"
RewriteRule ^ /insights [R=301,L]
which does work, however, I don't really want to have multi-line rewrites for lots of URLs, and would like to understand why the other 2 examples do not work.
You just need to insert this rule before last catch-all rule.
RewriteRule ^/?news/?$ /insights [R=301,L,NC]
Place it just below RewriteEngine On line so that mod_rewrite engine executed this rule before other rule.
Make sure to test it in a new browser.
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.
I am trying to change the url that is displayed in the address bar from mysite.com/blog/wedding-hair/ to mysite.com/services/wedding-hair/ using .htaccess.
Using answers from:
https://stackoverflow.com/questions/8713319/assigning-different-name-to-existing-folder-in-url-in-htaccess
rewrite a folder name using .htaccess
Replace directory name in url with another name
I added to the .htaccess file. Here is the .htaccess file, I added the last rewrite rule:
Options -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule ^/?$ "http\:\/\/www\.mysite\.com" [R=301]
RewriteRule ^blog/(.*)$ /services/$1 [L]
the non-www redirect works but not the blog-services rewrite. I thought maybe I had the directory names reversed but changing them around doesn't work either. I have tried adding and removing /'s around the directory names in all of the different combinations. I tried adding
RewriteCond %{THE_REQUEST} ^GET\ /blog/
before my RewriteRule. Nothing I Have tried has worked, the displayed url remains mysite.com/blog/wedding-hair/
I am sure this is pretty straight forward for someone but I am unable to get this correct. Any help would be appreciated.
When I was working on this yesterday I didn't think about the fact that the blog directory is a WordPress install. Here is the .htaccess file that is in the blog directory:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
I have tried adding my RewriteRule in this file but still no joy.
The problem here is that RewriteRule ^blog/(.*)$ /services/$1 [L] internally rewrites the URI, so that the browser doesn't know it's happening, this happens entirely on the server's end. If you want the browser to actually load a different URL, you need to use the R flag like you are in your www redirect, though it's only redirecting requests to root. If you want it to redirect everything to include the "www", you want something like this:
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Then to redirect "blog" to "services", just add the R flag (or R=301 if you want the redirect to be permanent).
RewriteRule ^blog/(.*)$ /services/$1 [L,R]
And, if for whatever reason your content isn't actually at /blog/, you need to internally rewrite it back
RewriteCond %{THE_REQUEST} ^GET\ /services/
RewriteRule ^services/(.*)$ /blog/$1 [L]
But this is only if your content is really at /blog/ but you only want to make it appear that it's at /services/.
Actually, in such case, as you have a specific field in Wordpress options to handle the display of a different url, it CAN'T work with .htaccess is the WordPress rules are executed at the end.
And it would be much simpler to use the field "Site Address (URL)" in the General Settings, and enter "mysite.com/services/"
If you don't do that, in spite of your .htaccess, the WP internal rewriting will use you installation repertory
I'm looking to do a .htaccess file that let any requests pass to its original destination but redirects to a specific folder (a splash screen in this case) is no destination is specified. I'm not very well-versed with .htaccess and would appreciate some help.
Example: I'm requesting http://www.domain.com/folder/file.php, it should go through. But if I'm requesting http://www.domain.com/, it should redirect to http://www.domain.com/splash/.
What I have so far redirects correctly to /splash/, but redirects everything to /splash/.
<IfModule mod_rewrite.c>
RewriteEngine On
# If the requested URI is empty...
RewriteCond %{REQUEST_URI} !^$
# ...then redirect to the "splash" folder
RewriteRule .* splash [L]
# Otherwise rewrite the base
RewriteBase /
# If the request is not a folder or a file, redirects to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Thanks!
EDIT: One important point would be to redirect http://www.domain.com/ to http://www.domain.com/splash/, but allow direct access to http://www.domain.com/index.php.
The %{REQUEST_URI} variable includes a leading slash, so it will NEVER be blank. You can get rid of that and just use this rule:
RewriteRule ^/?$ /splash/ [L,R]
If you want the URL that appears in the browser's address bar to stay http://www.domain.com/, then remove the ,R from the square brackets: [L].
I want to rewrite /home to the index.php and /edit to edit.php, but everything else, like /asdfg I want to redirect to new.php
Both edit and home on their own work, but the (.*) takes priority over those rules, I tried it without the RewriteCond, but then nothing works.
RewriteEngine on
RewriteBase /
RewriteRule ^home$ /index.php
RewriteRule ^edit$ /edit.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /new.php [L,R=301]
I just need all undefined URI's to redirect to /new.php
I also need to keep the /asdfg in the URI, so I can grab it with JavaScript and show the appropriate content, an either show a page to let ppl make a new post or to show existing content.
I can't define all existing content, because that comes from a database, only a few predefined, reserved "/folders" will redirect to predefined pages.
I have the following working:
RewriteEngine on
RewriteRule ^home$ /index.php [R]
RewriteRule ^edit$ /edit.php [R]
It leaves the /home there and go to index.php
Now I just need to redirect all unknown requests to a default page new.php.
I basically want to prevent users getting a 404 or any other error, ever..
You need to add the last flag to your first RewriteRules, so that the last RewriteRule does not take effect:
RewriteRule ^home$ index.php [L]
RewriteRule ^edit$ edit.php [L]
Try adding the [L] flag to the "home" and "edit" rules, so that the "new" rule isn't processed once they're matched.