RewriteRule for SEO in a SPA not working - apache

I wish to implement SEO for my single page application website (assume it to be at www.myexample.com)
After reading Google's documentation (https://developers.google.com/webmasters/ajax-crawling/docs/getting-started) I started by attempting this for the website's home page.
That means,
www.myexample.com?_escaped_fragment_=
should 'map to'
www.myexample.com/staticIndex.html
I added the following to .htaccess file in the website's public_html folder:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=
RewriteRule ^_escaped_fragment_= /staticIndex.html [PT]
After this change, when I try the URL www.myexample.com?_escaped_fragment_= in browser, I expected the browser to show me content of file staticIndex.html
Instead, it shows me content of www.myexample.com (index.html) and the browser URL remains unchanged to www.myexample.com?_escaped_fragment_=
Could someone help me with the RewriteRule?
Thanks.

try this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=$
RewriteRule (.*) staticIndex.html [PT]

Related

htaccess rewrite root url to subfolder with accessing other folder?

I have following folder structure in apache server.
Public_html
-->admin
--->admin_login.php
-->website
--->index.php
since the index.php inside the website folder,
i have given following code in .htaccess
RewriteCond %{REQUEST_URI} !^/website/
RewriteRule ^(.*)$ /website/$1 [L,NC]
so that the when the user enter root url , it will appear "www.myurl.com" instead of "www.myurl.com/website/"
but the issue is, i could not be able to access admin_login.php.
is there anyway to modify .htaccess, to come website/index.php in main url and able to access admin_login.php(both)?
Thanks in Advance
You need to add an exception to existing rule like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(website|admin)/ [NC]
RewriteRule .* website/$0 [L]
Negative condition %{REQUEST_URI} !^/(website|admin)/ will match every URI except URIs that start with /website/ or /admin/. This will allow you to directly open www.myurl.com/admin/admin_login.php.
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php/?$ website/index.php [QSA,NC,L]

Url rewrite showing the real directory

I have read almost everything and in theory this should be correct. However the redirect shows the real directory in the url when I look for subdirectories.
RewriteCond %{HTTP_HOST} ^myhost\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/tool [NC]
RewriteRule (.*) /tool/$1 [L]
If I go to http://myhost.com there is no problem, but when I try to go to http://myhost.com/subfolder/ this redirects to http://myhost.com/tool/subfolder/ instead of letting me see the content at the requested url http://myhost.com/subfolder/
Btw the content is loaded my problem is just the url bar.
In other words it works on the base folder but fails when I add something below.
http://myhost.com OK
http://myhost.com/anything OK it redirects to http://myhost.com/tool/anything (I don't want to see /tool/)
http://myhost.com/anything/ (trailing slash) KO! it redirects to
http://myhost.com/tool/http:/myhost.com/var/www/html/anything

Redirecting All Requests to Index.html

I would like to redirect all requests made to my website to index.html if the request aren't made from AJAX. Apart from just redirecting, I would like to append the request URI as a get parameter to index.html, for example if someone visits http://example.com/something.html, the person should be redirected to http://example.com/?origin=something.html. To achieve this, I have the following code in a .htaccess file
RewriteEngine On
RewriteCond %{HTTP:X-Requested-With} !=XMLHttpRequest
RewriteCond %{HTTP:X-REQUESTED-WITH} !^(XMLHttpRequest)$
RewriteCond %{REQUEST_URI} !^/(index.html)?$ [NC]
RewriteRule ^/.*.html$ /index.html?origin=%{REQUEST_URI} [R=301,L,NC]
However, it does not redirect any requests as expected. I'm on Apache/2.4.33, Ubuntu 16.04. What am I doing wrong?
As CBroe pointed out, I was facing the issue because the context of .htaccess has the initial / removed from %{REQUEST_URI} that is usually the conditional part of RewriteRule. Therefore, the solution was only having to change the RewriteRule to:
RewriteRule ^(.*).html$ /index.html?origin=%{REQUEST_URI} [R=301,L,NC]

How redirect a page with htaccess and relative requests?

I need to type on browser
mydomain.me/myproject
instead to type
mydomain.me/fold1/fold2/yymmdd-myproject
It works after I've added the code below in .htaccess:
RewriteEngine On
RewriteRule ^myproject/?$ fold1/fold2/yymmdd-myproject/ [NC,L]
But now the requests from the index page (css, script sources...) call them from the domain's root (mydomain.me, not from mydomain.me/myproject/) and, of course, the server responds with "not found".
How can I fix it?
UPDATE 1
I tried to write this (just added the domain in path):
RewriteEngine On
RewriteRule ^myproject/?$ http://mydomain.me/fold1/fold2/yymmdd-myproject/ [NC,L]
and now the page loads correctly with css and other libraries, but the url in address bar is mydomain.me/fold1/fold2/yymmdd-myproject/ instead like mydomain.me/myproject as I read in Apache document.
I solved in this way:
RewriteEngine On
RewriteRule ^myproject$ http://mydomain.me/myproject/ [NC,L]
RewriteRule ^myproject/(.+)?$ fold1/fold2/yymmdd-myproject/$1 [NC,L]
Two lines per project.

how to redirect a subdomain with child directory to new url using htaccess

I have a old website which used subdomains for each pages, i'm moving to new site and want to redirect the most of the links to the new pages.
I searched a lot but didn't find anything.
For instance i will be redirect this url to this:
blog.test.com/12/09/test-2 to test.com/blog/test-2
How it possible?
I tried this
or just simply redirect all the blog.test.com with whatever query they have to the new page?
blog.test.com/whatever?whatever to test.com/blog
You can use this code in your DOCUMENT_ROOT/.htaccess file of test subdomain:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(blog)\.(test\.com)$ [NC]
RewriteRule ^\d{2}/\d{2}/(.+)$ http://%1/blog/$1 [L,R=302]