URL issue while migrating website from root domain to sub directory - html

I have a situation as follows:
I have a website with lots of files in it (Yii Framework). I need to migrate it from http://domain1.com to http://domain2.com/foo/bar/
But previous developer has put the href, src, background-image etc links as follows:
href="/assets/img/img1.jpg"
src="/assets/js/script.js"
When I open the new website in browser, all the resources should be loaded like this http://domain2.com/foo/bar/assets/... to make it work. But, browser is interpreting the resources url as http://domain2.com/assets/...
As the resources doesn't exists here, they aren't loading.
As the urls are scattered everywhere in lots of files, it's not the best idea to change each and every url.
Is there a way to change the base url through htaccess (or some other method) so that server or browser will interpret href="/assets/..." as http://domain2.com/foo/bar/assets/...

You may use this 301 redirect rule at the top of other rules:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain1\.com$ [NC]
RewriteRule ^assets/.+ http://domain2.com/foo/bar%{REQUEST_URI} [L,NC,NE,R=301]

Related

.htaccess rewrite cond for adobe muse

I have an adobe muse site that office staff update. However I want to add an html file at the top level so that I have example.com/recruitment-advertising.html minding its own business and just working separate from adobe muse.
I have tried to exempt the file from muse in the .htacess file using a condition but the page keeps coming up
Page Not Found
We could not find the Web Page you requested. This is either because:
There's an error in the address or link. Or you have entered the
address or link incorrectly.
It is probably syntax, but I can't seem to get it to work.
My .htaccess file currently looks like
# Begin Muse Generated redirects
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/recruitment-advertising\.html$
RewriteRule ^muse_test_redirect.html$ muse_test_redirect.php [L]
</IfModule>
# End Muse Generated redirects
I don't think relevant but all the resources for that page are in a lower folder at example.com/landing
I want the page at top level, because its a landing page and I don't want it several folders in as google doesn't think things that deep are so important.
I don't care if the solution is something entirely different and not using .htaccess, I just want my html page to run on the domain. Many thanks in advance.

Folder Organization for Website

In my website, I have a home HTML file. Then, when I click on something in the nav, for example Roster, the url changes to http://www.nextgengaming.org/Roster/roster.html. How can I stop this? I'm not so worried about the extension, as I can get rid of that later. Here is my file/folder setup that I use when building websites (please note that I have nothing in the html file for 'Roster', because I am focussing on the url right now):
Website
∟ Home
∟ CSS
HEADER (social icon images)
home_files (for my image slider)
IMG (the rest of my images)
home.html
home.js
∟ Roster
∟ roster.html
I will fill the roster with pretty much the same things. In my nav I have
<li>Roster</li>
so the URL is (as said before) http://www.nextgengaming.org/Roster/roster.html, and want to change it to
http://www.nextgengaming.org/Roster
OR
http://www.nextgengaming.org/roster.html
If you need any more information, please comment. Also, I'm not sure if this is the right place to post, but I'll take my chances.
The url changes to http://www.nextgengaming.org/Roster/roster.html How can I stop this?
The suggested ways would be to edit VirtualHost or .htaccess by adding
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]
Note that rewrite module must be enabled (sudo a2enmod rewrite). Speaking of DirectoryIndex (documentation here) it searches for
index.html index.htm default.htm index.php index.php3 index.phtml index.php5 index.shtml mwindex.phtml
And if none of the resources exist and the Indexes option is set, the server will generate its own listing of the directory. Indexes may cause a security threat (exposing all the files) so use
Options -Indexes
to avoid that. If following the instructions, the
http://www.nextgengaming.org/Roster/roster.html
would be rewritten to
http://www.nextgengaming.org/Roster/roster
To avoid the Roster/roster and having just roster put the roster.html in your DocumentRoot probably right next home.html.
Simply use the default extensions (index.html usually), and change the URLs to: http://www.nextgengaming.org/Home and http://www.nextgengaming.org/Roster
The structure would be:
Website
∟ Home
∟ CSS
HEADER (social icon images)
home_files (for my image slider)
IMG (the rest of my images)
index.html
home.js
∟ Roster
∟ index.html
If you don't like renaming files like that, a more advanced approch would be to use URL rewriting (using .htaccess, for example).

Webpage redirect

My website had been hacked and lots of pages had been added and these have been indexed by Google. This has seriously affected the amount of traffic my site is receiving.
These pages all are all named along the lines of - '?mulberry-948.html' just with a different number for each.
I have deleted all these pages but there are back links to these from lots of websites around the web so Google is still looking for them.
Is it possible to redirect all of these pages use .htacess in a simple way without having to add a redirect for each file.
i.e can i say for anything that begins with '?mulberry' to be redirected to index.html?
Thanks
can i say for anything that begins with '?mulberry' to be redirected to index.html?
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^mulberry [NC]
RewriteRule ^ /? [L,R=301]
? in the end will strip off any existing query string.

How to link to pages without the .html extension?

I would like to link to pages inside my website, e.g:
Not: mywebsite.com/about.html But: mywebsite.com/about/
I've seen various websites doing this but it looks like they also react differently to things:
Apple.com:
apple.com/iphone/ works, apple.com/iphone/index.html works, apple.com/iphone redirects.
Opera.com:
opera.com/mobile/ redirects, opera.com/mobile works, opera.com/mobile.html does not work.
Mozilla.com:
mozilla.org/en-US/ works, mozilla.org/en-US redirects, mozilla.org/en-US/index.html does not work.
Which leads to another question: Are there different methods for this?
Edit:
It seems that Apple uses a folder for every page, e.g. a folder called 'iphone' with an index.html file inside it?
But Opera and Mozilla use something in the .htaccess file?
Removing Extensions
To remove the .php extension from a PHP file for example yoursite.com/wallpaper.php to yoursite.com/wallpaper you have to add the following code inside the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
If you want to remove the .html extension from a html file for example yoursite.com/wallpaper.html to yoursite.com/wallpaper you simply have to alter the last line from the code above to match the filename:
RewriteRule ^([^\.]+)$ $1.html [NC,L]
That’s it! You can now link pages inside the HTML document without needing to add the extension of the page. For example:
wallpaper
They are using .htaccess and URL rewriting. This is part of server configuration. You can not do it with html only.
This page explains basics of URL rewriting.
You folder then has to contain a file: index.*.
Like: /iphone/index.html, which can be /iphone/ as well
Or work with .htaccess
In the .htaccess file in your sites root folder just add the following line:
# ---- Render pages without urls
Options +MultiViews
The most upvoted answer doesn't check whether the URL points to a directory, so you're going to get some mysterious 'not found' errors when it tries to append '.html' to a directory path. Easily fixed:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [L]
The first condition will only pass if the path does not point to a valid directory. The second will only pass if the path points to a valid file when the .html extension is added. If both conditions pass, the rewrite rule simply adds ‘.html’ to the filename.
Notice that we can just match the entire path with .*. You can reject paths that contain a period character if you wish, but it's not really necessary since you've already checked that {REQUEST_FILENAME}.html is a valid file. In any case, it is unnecessary to escape a period character when it's inside a character class. I know you see this [^\.] everywhere, but the slash is redundant. [^.] is how to write it and look like a regex pro. 😎
This kind of redirect will be invisible to the user because, by default, mod_rewrite does the substitution internally, without informing the browser. If you wanted to do a permanent redirect, you would add the [R=301] flag at the end.
Alternatively, as Genus Amar said, you can just enable the Multiviews option on a per-directory basis by adding this Options Directive to the .htaccess file:
Options +MultiViews
It's worth adding that this will only work if the server administrator has enabled MultiViews with the AllowOverride Directive, and it won't allow you to perform additional redirects.
Neither of these solutions (on their own) will remove the .html if it’s part of the requested URL. If you want to do that as well, see my answer to this question.
Make your href attribute equal to the page you want to link or .. If you need to
move up a directory.
Ex: href="contact.html"
Ex: href="../links/contact.html"

Can we block opening CSS, JS files?

It is very common we link CSS and JS files in out HTML, PHP pages. Can we block opening of the CSS and JS files directly from a browser. Since the source code can be viewed by anyone, he/she can open those files by understanding the path. How can we achieve blocking these files?
You need to setup your web server to server the static content (js, img, css) only if refered by your host (looking at the http headers), but it won't totally prevent user from doing it. as for the php users won't see it, it runs on the server, and will output most of the times inert html.
A basic block would force the browser to send a valid Referer header when accessing the files. This can be done with some simple .htaccess:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !your\.domain\.here [NC]
RewriteCond %{REQUEST_FILENAME} \.(css|js) [NC]
RewriteRule .* - [L,F]
However this is not a great idea because it's easy enough to fake the referrer, or add a link to the page so that the browser naturally sends it. Also, some browsers just don't show the referrer header.
The browser has to be able to access those files in order to properly display the page. You can obfuscate the JS, either through something basic like minification, or something more complex like How can I obfuscate (protect) JavaScript?
With CSS, you can try something similar.
Look into the following resource, they might help.
http://www.iwebtool.com/html_encrypter
http://www.catswhocode.com/blog/3-ways-to-compress-css-files-using-php
http://refresh-sf.com/yui/
http://www.n1studios.net/tutorials/php/css-file-protection.html
all of them are somehow way around, and there is always a way to read them. you can only make them hard to read