angular ui.router html5mode unable to reload in apache - .htaccess

I'm trying to remove '#' from urls.
Using link with ui-sref or href attributes navigation works properly, but accessing directly to the same url, or refreshing page or pressing enter into url bar of the browser i get 404 not found error.
I enabled html5 mode:
$locationProvider.html5Mode(true);
Added base tag in head before any inclusion:
<base href="/folder/" />
Here is .htaccess file content (copied from angular-ui faq):
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ /folder/index.html [L]
Seems to be a problem related to .htaccess file.
What is wrong?

I've to:
enable AllowOverride in /etc/apache2/apache2.conf (enable AllowOverride)
enable mod_rewrite, from terminal a2enmod rewrite
restart apache from terminal service apache2 restart

Related

WordPress Cannot save Permalink shows 404 error

I have hosted a domain in Linux Apache 2.4.16 web server and installed WordPress in it. Now I cannot update Permalink from default (date/time/%postname%/) to /%postname%/ or custom structure. Only Plain type gets saved. I am getting 404 error.
404 Page not found
I have cleared everything in the server contents, posts, plugins, even ssl too and remove and install WordPress,... But I am still getting the same error.
Any suggestions?
Seems all the things you did are correct in this context. Have you checked whether the mod_rewrite module is on in Apache. You can check that by writing the phpinfo function in a file at your server.
Check Screenshot
Read
Give permission to .htaccess file. Through Linux command line. At first navigate to the project folder,
cd /var/www/html/project-folder
Then give 777 permission to .htaccess file. By this below command,
sudo chmod -R 777 .htaccess
Then edit .htaccess file with the below code.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
And then update the permalink settings 2 times from the wordpress admin panel.

Refresh page on apache with React doesn't work

I'm trying to use React to create a SPA, but I'm running into problem when trying to reload the page.
Before I continue, I must say I already read these questions this and this. I manageg to made it work, but only for the first url.
For example, I have a blog page containing all the posts, accessed via this url mysite.com/blog. This one works fine, if I refresh the page, everything reloads again.
However, when I try to access a single post using a dynamic url, then the page doesn't work. For example, I have this router setup:
// Definiton
<Route path="/post/:url" component={Post} />
// Link
<NavLink to={"post/" + post.url}>...</NavLink>
// Url on the browser
mysite.com/post/this-is-my-first-post
In this case, it's not working. This is my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
After looking more at the problem, I noticed it's trying to load the main files from a different location. For example, when I'm on the page mysite.com/blog it's loading the .css and .js files from mysite.com.
But when I'm the page mysite.com/post/this-is-my-first-post and I refresh the page, it's trying to load the .css and .js files from the directory mysite.com/blog.
I did what Stuffix told in the answe, to check the url definition and also the config at my apache, but everything is enabled and working, just like the answer says.
Well, after looking at some other projects I have using Angular, I noticed one thing, and it was easier than I tought.
Just had to add the tag base on the head of my index.html.
<base href="/" />
This htaccess worked for me with react and angular
<IfModule mod_rewrite.c>
RewriteEngine on
# -- REDIRECTION to https (optional):
# If you need this, uncomment the next two commands
# RewriteCond %{HTTPS} !on
# RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# --
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) index.html [NC,L]
</IfModule>
In your <Route />, you're declaring the post route is /post/this-is-my-first-post which is correct.
But in your <NavLink />, you're pointing towards /blog/post/this-is-my-first-post since you've missed a slash.
<NavLink to={"post/" + post.url}>...</NavLink>
| here
Thus leading to a 404.
If this doesn't help, your snippet looks fine so the problem might be on what does post.url returns.
Also, your .htaccess looks fine so I would say you might have missed something in your Apache config like AllowOverride all. See this answer.

How to write .htaccess rewrite rule to solve 404 error when angularjs html5mode is enabled

I am new to AngularJS and PHP. In the application i am working on, I have enabled html5mode in angularjs using following things:-
-- Added base href in /myapp/index.html
<base href="http://localhost/myapp/" />
-- Added following at the end of config function.
$locationProvider.html5Mode(true);
When I access my application using following url: http://localhost/myapp ,
It does not append any # in browser url. I can navigate to any other route successfully. Route url is http://localhost/myapp/category/1
But when I refresh the page, above url gives me 404 error. I understood that, server trying to find file at location /myapp/category/1. I read at other places writing .htaccess rule will solve this problem. I tried all those options. Nothing worked for me.
I am using xampp and location of .htaccess file is /htdocs/myapp/.htaccess
.htaccess file is accessible, because if I add any junk data in it, url throw error.
Please help me on this. I need server to return url as it is so that angularjs can route pages correctly. Please help me to write .htaccess rule. mod_rewrite is already enabled in httpd.conf file of apache.
I can able to find solution for this. My .htaccess file will look like below
Options +FollowSymLinks
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index.html
RewriteCond %{REQUEST_URI} !.*\.(map|cssĀ¦js|html|png)
RewriteRule (.*) index.html [L]
</ifModule>

.htaccess cans and can'ts

I am very new to the idea of .htaccess and thought that it was what you used to do something like turn this:
http://www.domain.com/some/ugly/url/here.html
into this:
http://www.domain.com/niceurl
I was just told by my ISP that in order to get that to happen, no, it's done by putting the document into the web root folder. That .htaccess isn't used at all.
Does anyone know if this is true? I see a lot of examples about what .htaccess DOES but not so much about what it can't do. Somehow I thought this was all that was needed.
Lastly, if someone types in www.domain.com/niceurl what will happen? Don't I need to have that linked (if not by htaccess, how?!) to the location of the actual file?
Thank you for any and all help. I realize that .htaccess questions abound but they're hard to pick through for the layperson and I'm hoping to answer this specific question.
Here's what I believe should be an answer you want, put the block below to your .htaccess
Answer:
## Enabling Apache's Mod_rewrite module.
RewriteEngine On
# Following line is required if your webserver's URL is not directly related to physical file paths (just / for root, e.g. www.domain.com/)
RewriteBase /
# Restricts rewriting URLs only to paths that do not actually exists
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# Redirect www.domain.com/bar to www.domain.com/foo
Redirect 301 /bar /foo
# Internally load the long URL without changing URL in address bar
RewriteRule ^foo/?$ http://www.domain.com/some/ugly/long/thing/here.html [L,NC]
As a result, www.domain.com/bar will be redirected to www.domain.com/foo and /foo will internally load http://www.domain.com/some/ugly/long/thing/here.html
FYI:
Your website's URL doesn't have to be directly related to physical file paths. Your URL's segment can be served as alias to your URL's parameters. for e.g,
http://www.domain.com/index.php?key1=value1&key2=value2
can be represented as
http://www.domain.com/value1/value2
Note: you need to implement a server side script to be served as a
router to manipulate the URL segments.
For more information about using .htaccess, check this out
Ref: http://htaccess-guide.com/
.htaccess files can be used to alter the configuration of the Apache Web Server software to enable/disable additional functionality and features that the Apache Web Server software has to offer. These facilities include basic redirect functionality, for instance if a 404 file not found error occurs, or for more advanced functions such as content password protection or image hot link prevention.
Below is a few examples,
# Custom Error Pages for Better SEO,
# for e.g, to handle 404 file not found error
ErrorDocument 404 http://www.domain.com/404page.html
# Deny visitors by IP address
order allow,deny
deny from 122.248.102.86
deny from 188.40.112.210
allow from all
# Redirects
Redirect 302 /en/my-dir/my-page.html /en/my-path/example.html
# Disallow some silly bots from crawling your sites
RewriteCond %{HTTP_USER_AGENT} (?i)^.*(BlackWidow|Bot\\ mailto:craftbot#yahoo.com|ChinaClaw|Custo|DISCo|Download\\ Demon|eCatch|EirGrabber|EmailSiphon|EmailWolf|Express\\ WebPictures|ExtractorPro|EyeNetIE|FlashGet|GetRight|GetWeb!|Go!Zilla|Go-Ahead-Got-It|GrabNet).*$
RewriteRule .* - [R=403,L]
# Setting server timezone
SetEnv TZ America/Los_Angeles
# trailing slash enforcement,
# e.g, http://www.domain.com/niceurl to http://www.domain.com/niceurl/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !#
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.domain.com/$1/ [L,R=301]
Enable mod_rewrite and .htaccess through httpd.conf (if not already enabled) and then You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^niceurl/?$ some/ugly/url/here.html [L,NC]
This will allow you to use http://domain.com/niceurl in your browser and it will internally load http://domain.com/some/ugly/url/here.html without changing URL in browser.
If you also want to force redirection from ugly URL to pretty URL then add this redirect rule just below RewriteEngine On line:
RewriteCond %{THE_REQUEST} \s/+some/ugly/url/here\.html [NC]
RewriteRule ^ /niceurl [R=302,L,NE]

htaccess rewrite all to index.html

I'm trying to write the .htaccess file so that whatever the user requests, he will have the page index.html
I've written this:
Options +FollowSymlinks
RewriteEngine on
RewriteRule .* index.html [NC]
I understand that this will cause: whatever the incoming request URL is, i.e, www.domain.com/*** (whatever comes after the slash), the result will be the page www.domain.com/index.html
However, I'm getting a Server error. What am I missing?
NOTE: I don't want it to be permanent redirect, I'm just trying to "hide" the content of my site for a couple of hours with that index.html page (which says that the site is under maintenance).
If you want to redirect everything to temporary maintenance page, you can do :
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/maintenance.html$
RewriteRule .* /maintenance.html [L,R=302]
the R=302 flag is used to generate a temporary redirect
Try to remove Options +FollowSymlinks , some servers won't allow you to overwrite a php.ini setting.

Resources