How redirect a page with htaccess and relative requests? - apache

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.

Related

Display content based on URL

In my site I had a desire to make simpler URLs such as:
http://www.example.com/about
Instead of:
http://www.example.com/about.php
Mostly for aesthetics, for now I have Apache redirect to ./default.php if a directory is requested. However this forces me to create a directory at
http://www.example.com/about/ and a file inside it called default.php ending up with:
http://www.example.com/about/default.php
I know there's a better way, probably using PHP or JS, what do?
it's all in apache's configs,
rewriteEngine can do that for you
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/
A snippet taken from third link:
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 to your Apache config:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
If you want to remove the .html extension:
RewriteRule ^([^\.]+)$ $1.html [NC,L]
That’s it! You can even link pages inside the HTML document without needing to add the extension of the page. For example:
wallpaper

force SSL for single .html page without php or anything else just using .htaccess

After several hours of trying a myriad of suggestions for .htaccess I have given up and decided to ask here.
I have a single html page that needs to be served via SSL. It is a single file with the .htm extension and it contains no php whatsoever. If anybody accesses this page via typing it in or clicking on a link from a non SSL page, I want that person to be redirected to or shown the SSL version of that page. Only https://example.com/myfile.htm should be allowed. The rest of the site can go without SSL, just this one page needs it.
Please help.
Try this in your .htaccess file.
RewriteEngine On
RewriteCond %{HTTPS} !^on$
RewriteRule ^myfile\.htm$ https://www.example.com/myfile.htm [R=301,L]
Try:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^myfile\.htm$ https://example.com/myfile.htm [L,R]
SSL for HTML only page / SSL for JavaScript only page:
If anyone needs to set SSL (HTTPS) for page that uses html only (without PHP, nodeJS etc.) just put .htaccess file in the same folder as index.htm page.
Content of the .htaccess have to be:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I had trouble finding how to, so I think that this thread is the best to hold this answer.

Using Apache mod_rewrite to send all requests to a file

How can I use mod_rewrite to send certain types of incoming urls to a particular file on the server?
Ideally any html requests would load a file (eg /var/www/sites/urlprocessor.php) which would then determine what is being requested and the display the appropriate content based on the url.
Example: Say I wanted a url to list some 'cars'. I would want the following URLs to load the urlprocessor.php file instead of loading the actual page and instead of doing a redirect.
http://www.mydomain.com/cars.html --> /var/www/sites/urlprocessor.php
http://www.mydomain.com/cars.htm --> /var/www/sites/urlprocessor.php
http://www.mydomain.com/cars --> /var/www/sites/urlprocessor.php
On the flip-side, non-html requests (jpgs, pdfs, etc) would continue on normally without loading the urlprocessor.php file.
I am currently using the last line of code in the following to accomplish this. It 301 redirects all html page requests to 'index.html' which then loads the urlprocessor.php file. I prefer to just load the file without the 301 redirect.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain\.com
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
RewriteRule !^.*(\.gif|\.GIF|\.png|\.mp3|\.m3u|\.js|\.jpg|\.JPG|\.css|\.swf|\.flv|\.ico|\.wav|\.doc|\.DOC|\.pdf|\.PDF|\.xls|\.XLS|\.txt|\.TXT)$ index.html [L]
Is there a way to go directly to a file without doing the redirect?
Thanks!!
Tom
Given that /var/www/ is the root folder then this would work:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /sites/urlprocessor.php [L]
Basically this will redirect any non-existent folder and file to the urlprocessor.php
For more information on how to handle the redirect with your PHP you can refer yourself to this answer.

Hide the directory in the address bar of the browser

The file .htaccess has this directive:
RedirectMatch permanent ^$ /moby/
When I type the website address into the address bar in the browser: http://mobyx.net/ it appears the directory redirected to: http://mobyx.net/moby/
How to hide the directory?
You are currently doing a redirect (you tell the client to try again on mobyx.net/moby if they go to mobyx.net). What you actually want to do is an internal rewrite. This is invisible to the client. You can use RewriteRule for that. See the documentation for more information.
First make sure that mod_rewrite is enabled in httpd.conf. Then add the following to your .htaccess
RewriteRule ^$ /moby/ [L]
This will internally rewrite http://mobyx.net/ to http://mobyx.net/moby/, but not http://mobyx.net/index.php for example. If you want everything to be rewritten on your site, use:
RewriteCond %{REQUEST_URI} !^/moby
RewriteRule ^(.*)$ /moby/$1 [L]
This will rewrite the url if it doesn't already point to the /moby subdirectory.

RewriteRule for SEO in a SPA not working

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]