i have a little problem. I have a website built with angularjs and my urls has a #.
I used html5 mode to remove it. Now i want to make some changes in the htaccess file but i have a problem with my url parameters.
This is my url:
http://myname.co/infosystem/www/app/#/static/randomcompany
and i want this url:
http://myname.co/infosystem/www/app/static/randomcompany
And my htaccess looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) http://myname.co/infosystem/www/app/static/ [R=301,L]
i think removing the hash works. but i have no idea how to get the 'randomcompany'.
i just want to take the parameter and pass it equal to the new url. Without adding something. Just the /static/abc /static/xyz
It would be very nice if someone could help me :)
Related
Found a problem with my site on NextJS. During development, I navigated the site using buttons and manually changing the browser address bar. It happened that I accidentally added a slash to the end, but my localhost server removed it and everything worked fine.
But everything changed when I uploaded my static application to the hosting. It automatically began to add these slashes when reloading the page. Because of this, my pictures on the site break.
As far as I understand, you need to correctly configure the .htaccess file.
Here is what it looks like now:
RewriteEngine On
RewriteRule ^([^/]+)/$ $1.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.html
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteRule ^([^/]+)/$ $1.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.html
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Your existing rules are all expecting (or forcing) a trailing slash on all your URLs. So, if the canonical URL (and the URL you are linking to) does not include a trailing slash then all these rules essentially need to be reversed. However, there are other issues here (the first rule, for instance, is unconditionally rewriting the request to append the .html extension, which is repeated in the next rule with a condition.)
Try the following instead:
RewriteEngine On
# (OPTIONAL) Remove trailing slash if it happens to be on the request
# Exclude physical directories (which must end in a slash)
RewriteRule %{REQUEST_FILENAME} !-d
RewriteRule (.+)/$ /$1 [R=301,L]
# Rewrite request to corresponding ".html" file if it exists
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^([^.]+)$ $1.html [L]
Your original directives only handled URLs with one or two path depth (eg. /foo/ or /foo/bar/). The second rule above handles any path depth (if so required). eg. /foo, /foo/bar, /foo/bar/baz etc. (no trailing slash).
As an optimisation I've assumed your URLs that require rewriting do not contain dots (that are otherwise used to delimit the file extension).
Note that the RewriteRule pattern (first argument) matches against the URL-path only (not the query string). If there is any query string on the initial request then this is simply passed through by default. (With regards to the rewrite and client-side JS, the query string is available on the initial request and should be parsed as before.)
Because of this, my pictures on the site break.
This will happen if you are using relative URLs to your images. You should really be using root-relative (starting with a slash) or absolute URLs to resolve this issue. See also:
404 not found - broken links to CSS, images
My folder structure:
--root
-----includes
-----content.php
-----index.php
-----sitemap.php
-----rss.php
i want using .htaccess file for routing my web application look like this:
http://mysite.domain/ => index.php
http://mysite.domain/any-slug/ => content.php
http://mysite.domain/sitemap.xml => sitemap.php
http://mysite.domain/any-slug.rss => rss.php
This is my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^sitemap.xml$ sitemap.php
RewriteRule ^(.+).rss$ rss.php?slug=$1
RewriteRule ^(.+)$ content.php?slug=$1 [QSA,L]
But, it not working. Somebody can help me?
I don't know if you've solved the problem but the .htaccess file just reroutes all the requests to index.php. From there you need to employ a router in order to show different pages depending on the url submitted.
For example:
User visits localhost/home/index/hello.
This gets redirected to index.php.
index.php uses $_SERVER['REQUEST_URI'] to retrieve the /home/index/hello explode it and require in the right page.
I have html5mode enabled so my urls look like domain.com/route and not like domain.com/#/route.
What I use is this rewrite code:
RewriteEngine On
Options FollowSymLinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /#/$1 [L]
It works fine with first level routes (domain.com/firstlevel) but it doesn't with secondlevel (domain.com/firstlevel/secondlevel).
Any ideas on how I can modify the rewrite code to put an "/#" before the whole url of any length?
Thanks
This might be a code problem however it might be getting encoded and maybe that's why it's not working for you. Typically if you don't escape the # it will encode it.
You can try it using the NE flag.
RewriteRule ^(.*)$ /#/$1 [NE,L]
Haven't found anything on this yet, but has anyone found out the right way to turn HTML5 mode on and have it work correctly with wordpress and it's current rewrites?
Wordpress rewrites:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Current wordpress rewrites
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) /index.php [L]
</IfModule>
And of course the apache HTML5 mode rewrites
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) /index.html [L]
I know their EXTREMELY similar but I'm having a hard time getting HTML5 mode to work on refresh while having a Angular APP embedded into a wordpress / php page.
After hacking on this, I came to a pretty hacky answer, YMMV:
RewriteRule ^search/([^/]+)$ http://www.example.com/search/#/$1 [NE,L]
In my current situation my app lives in example.com/search/
So your telling me you turned on HTML5 mode to essentially revert it back with a redirect?!
Yes.
Are you drunk?
Possibly, but I do have a couple reasons on why;
I can't redirect wordpress back to index.html, because well it will fail horribly.
What's the next best thing? A hash, because when html5mode is off it works perfectly fine!
So why even turn HTML5 mode on? Well I had to, I have a couple things going on in the background that I need crawlers to read etc.
Crawlers? Why not just use prerender.io or another framework? 1-Money 2-Resources 3- Can we start coining the term... Instead of throwing money at a problem, let's throw a framework at it.
I'm not going to accept my answer because well it's a hack, but I know alot of people are faced with this after doing some searching. Hopefully someone / we can come up with a better solution to this!
This seems to work just fine for me with WP. This will skip to the actual resource if there is one and to index.php if not.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.php [L]
Sample route which would render service if the url was /service or /home if the url was anything else.
$routeProvider
.when("/service", {
controller: "ServiceController",
templateUrl: "/wp-content/themes/angularjs-wp/templates/service/index.html"
})
.otherwise({
redirectTo: "/home"
});
$locationProvider
.html5Mode(true)
I am working on AngularJs with HTML5 mode enabled for SEO friendly URLs, its working fine with the in app navigation but on reload.
Have written below .htaccess but still facing the same issues for nested navigation
RewriteEngine On
RewriteBase /akv/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteCond %{REQUEST_URI} !.*\.(css|js|html|png|jpeg|gif|)
RewriteRule (.*) index.html [L]
Its working fine for fist level navigation like base/Blog or base/Portfolio but base/Blog/MyBlogTitle/type/post/id/1
Is there is any way to fix it.
Thanks in advanced.
Problem is that you're using relative URLs in your css/image/js paths. Relative URLs are resolved as browser adds them to the current path thus giving you 404 when using pretty URLs.
To fix this problem you can add this in your page's HTML <head> section:
<base href="/kv/" />
so that every relative URL is resolved from that URL and not the current page's URL.