Apache Rewrite to add html extension - apache

I am trying to get an apache RewriteRule to take a url and return the url + .html
Ex: 'www.domain.com/about' should serve the page 'www.domain.com/about.html'
I have tried numerous solutions from SO already but I believe there are some complications due to the site layout.
I am hosting on Network Solutions, so it's shared hosting, and this is a development site so I currently have the dev.domain.com pointed to /htdocs/dev
Directory Structure
-htdocs
--(contains existing sites files)
--.htaccess
----dev
------(contains dev site files)
------.htaccess
So as you can see there is an existing .htaccess file that maybe doing something and there maybe a subdomain issue, I'm not entirely sure, this is my first time working with Apache.
Here's my existing .htaccess which resides in the /htdocs/dev folder
RewriteEngine On
RewriteBase /dev
RewriteCond %{REQUEST_URI}.html -f [NC]
RewriteRule ^ %{REQUEST_URI}.html [L]
Thanks for the help!

You are instructing Apache to make a file check (-f), but it doesn't know where to look. Just change
RewriteCond %{REQUEST_URI}.html -f [NC]
to
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.html -f [NC]
That way Apache has a fully qualified path from your filesystem's root.

Related

MediaWiki rewrite url

I have made an installation in a Linux Debian of MediaWiki version 1.31 with Apache. I installed the installation in the path /var/www/html/wiki. All right, because when I open the browser and put http://<ip_server>/wiki, it correctly enters my MediaWiki installation.
What I now want is to redirect or rewrite http://<ip_server>/wiki to http://<ip_server>/ so that if I put in the browser http://<ip_server>/ it will redirect me to my MediaWiki installation.
Try the Short URLs documentation at https://www.mediawiki.org/wiki/Manual:Short_URL/Apache
Basically, you need to create a file called .htaccess in /var/www/html/ with the following content:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/w/index.php [L]
And add this line to your LocalSettings.php file:
$wgArticlePath = "/$1";
Note: The documentation suggests that you avoid using short URLs that are on the server root (like http://<ip_server>/) because things get complicated when you have pages named Images, Docs, Extensions, etc.

angular deployment on the apache server

I followed the steps outlined here.
I copied and paste the code below in .htaccess file. The .htaccess file is in the same repository that contains my dist folder that I am going to deploy on apache.
RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html
When I access localhost/dist everything goes fine, since it will use my index.html file and it will automatically redirect to localhost/dist/login. Now if I request directly localhost/dist/login, apache complains and displays The requested URL /dist/login was not found on this server. It seems for me that apache does not picked up my .htaccess settings. Is there something I am doing wrong?
Finally I found a way to solve my problem. Maybe this could help some. I enabled the rewrite mode of Apache and instead of
RewriteRule ^ /index.html,
I wrote
RewriteRule ^ /dist/index.html
and this solved my problem.

Invisibly rewriting root to subdirectory using mod_rewrite (ajaxplorer)

I've been looking on various sites reading mod_rewrite examples for a few hours, and have used it many times before successfully... but I'm trying something new with it now and can't for the life of me get it working!
I'm using a PHP file manager tool called AjaXplorer, I have it in a subdirectory on the root of my server /ajaxplorer-core-4.0.4. When I go to the root of my site http://domain.com/ I want it to invisibly redirect to the /ajaxplorer-core-4.0.4 folder (but still show the root domain in the address bar).
I still want to be able to access the other files/directories on the root as normal just typing in the path.
I assume this is possible? Seems relatively simple but I just can't get it working.
AjaXplorer seems to load js files and images etc from /ajaxplorer-core-4.0.4/plugins, I have a feeling that's where it's tripping me up.
Any pointers would be massively appreciated! Thanks
Found it on another answer luckily :)
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?site.com$
RewriteCond %{REQUEST_URI} !^/subdir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdir/$1
RewriteCond %{HTTP_HOST} ^(www.)?site.com$
RewriteRule ^(/)?$ subdir/index.php [L]
Here's the wiki of serverfault.com
The howto's htaccess official guide
The official mod_rewrite guide
And if that's not enough:
Two hints:
If you're not in a hosted environment (= if it's your own server and you can modify the virtual hosts, not only the .htaccess files), try to use the RewriteLog directive: it helps you to track down such problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

Apache Rewrite: favicon in %{HTTP_HOST} directory

My software supports multiple domains pointed at the exact same directory, I use shared hosting. I need to have each domain's favicon load from directories with their respective host names. Here is a visual...
http://www.example1.com/favicon.ico
public_html/www.example1.com/favicon.ico
\
http://www.example2.com/favicon.ico
public_html/www.example2.com/favicon.ico
\
http://www.example3.com/favicon.ico
public_html/www.example3.com/favicon.ico
I've tried some rewrites along the lines like this without any success...
RewriteEngine on
RewriteRule ^favicon\.ico$ %{HTTP_HOST}/favicon\.ico
Things to keep in mind...
1.) I use shared hosting so remember that the answer I need should be short and simple.
2.) I will only accept a DYNAMIC answer, I will only use the %{HTTP_HOST} variable and NOT a static domain name as I will not be manually editing my .htaccess file every single time I add a domain name.
3.) I may end up putting a .htaccess file in those sub-directories though I do not at the moment, an exception for the favicon would be greatly appreciated though is not necessary for me to accept the answer.
4.) I'll be more than happy to make any clarifications.
Use this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteRule ^(favicon\.ico)$ %{HTTP_HOST}/$1 [L,NC]
I have been struggling with this issue too but I finally fixed it using the following rule:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/favicon\.ico$ /sites/all/themes/mytheme/favicon.ico
</IfModule>
I stuffed this into a virtual host declaration. You can do this for each of your virtual hosts, all you need to do is point the second part to the correct icon!
This solves all of my favicon problems, even for Firefox :)
(Tested on FF25, Safari 6.1, IE8 and IE10)
This one worked better in my case
RewriteCond $0 !=images/favicon.ico
RewriteRule ^([^/]+/)*favicon\.ico$ /images/favicon.ico [L,NC]
To support all possible browsers and platforms, in addition to favicon.ico file, need to have files such as android-chrome-192x192.png, apple-touch-icon.png, favicon-32x32.png, etc...
Here is rewrite rule to support them all:
RewriteRule ^(favicon.*\.(ico|png)|apple-touch-icon.*\.png|android-chrome.*\.png|mstile.*\.png|safari-pinned-tab.*\.svg)$ /favicons/%{HTTP_HOST}/$1 [L,NC]
This will serve favicons including Apple Touch, Android Chrome, Windows and other favicons from /favicons/<DOMAIN_NAME> folder.
I had a problem with favicon in files on subdomain
I was struggling with redirect for favicon in htaccess for only one subdomain for a long time.
My case was that all domain take favicon from public/ directory. One subdomain (let's call it 'subdomain_a') is configured to take it from another directory and it works.
Problem appeared when a file was opened on subdomain_a. The favicon in file view (f.e. pdf-viewer) was taken from public/ directory, not from configuration of subdomain_a.
Here is my solution:
# Redirect for favicon
RewriteCond %{HTTP_HOST} ^www.subdomain_a.com
RewriteCond %{REQUEST_URI} ^/favicon.ico$
RewriteRule ^(.*)$ /path/to/favicon/for/new/domain/$1 [R=301,L]

Apache index.php redirect for base and sub directory

I am looking to run WordPress and Magento on the same domain. Wordpress is installed at the root of the site and Magento would be installed at /store/.
I have had the Apache config setup for WordPress (having all requests redirect to index.php), what Apache configuration logic do I need for all requests to /store/* to goto /store/index.php ?
You should be able to simply keep the default Magento .htaccess file in the store directory along with the other Magento files, then exclude anything with that path from your wordpress rewrites. I'm not up to date on what wordpress .htaccess files may look like, but it should simply be a case of adding a RewriteCond.
I would guess that the rewrite for wordpress will look something along the lines of...
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule (.*) /index.php?$1 [QSA,L]
If this is the case simply add another RewriteCond should do the trick...
RewriteCond %{REQUEST_URI !^store/
Posting the Wordpress .htaccess file may lead to more accurate advice.