How to rewrite to nonexisting directory - apache

On my webpage I've an Joomla Installation in /joomla, now I want to move the complete /joomla directory to / but old links with the joomla inside should be redericted to the new site at /. How is this possible?
In my .htaccess I've tried some stuff from somewhere out of the Internet, but the rules doesn't work, throws everytime an 404 HTTP Error.

Place this rule as your very first rule in /joomla/.htaccess:
RewriteEngine On
RewriteRule ^ / [L,R=301]

Related

Domain repeated twice in 404 error page when using htaccess

I am developing a .htaccess file for my site to clean up the urls. I am trying to rename my contact page to make sure its working but I keep getting a 404 error page saying that my url missing. The .htaccess files appears to be working fine but my domain is being repeated in the 404 error page. Here is my .htaccess file so far.
# Turn Rewrite Engine On
RewriteEngine on
# Rewrite for contact.php
RewriteRule ^message$ contact.php [NC,L]
Here is the 404 error
The requested URL /domain.com/domain.com/contact.php was not found
on this server.
Additionally, a 404 Not Found error was encountered while trying to
use an ErrorDocument to handle the request.
My .htaccess file is in the root of my site. It is probably a simple problem but I have been trying all the solutions I could find but to no avail.
What happens is, you are using relative links when rewriting URL. This causes the server to look for /domain.com/domain.com/contact.php.
Use absolute URL in rewrite:
RewriteRule ^message$ /contact.php [NC,L]

multiple domains with htaccess rewrite rule

I'm having difficulties getting an htaccess to work with a subdomain.
my server structure:
root / index.php ---//codeigniter index file, for application A, main domain points here.
root/staging/StagingWebsite ---// my subdomain is pointing here.
the folder StagingWebsite has a file called temp.html
moving on, my root htaccess file is this :
RewriteEngine on
RewriteCond
RewriteCond $1 ^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php?/$1 [L]
Which turns any access to MyDomain/foo to MyDomain/index.php/foo. (without showing the index.php).
The problem:
When I try to access my subdomain/temp.html, I get a 500 internal error.
when I remove my root/.htaccess, it works fine. So it's obviously my htaccess file.
I've figured out the problem is that the root/.htaccess rule is being applied to the subdomain, which breaks everything, But I have no ideahow to sort it out.
I've placed an empty .htaccess file inside the root/staging/StagingWebsite hoping it would just over-write any previous htaccess settings, But that didn't work.
EDIT
I fixed the issue specifically but I don't like the solution.
I added a RewriteCond to only run the rewrite rule for as specific domain.
Is there a way to solve this without specifying a domain?
Create /root/staging/StagingWebsite/.htaccess with this line only:
RewriteEngine on
This will overwrite any parent's rewrite rules.

Url rewrite not working; 404 error and no url change

I'm working on a website where I want the url
www.example.com/directory1/states/california.php
to point to
www.example.com/directory1/california
And a similar url change for the city pages as well:
www.example.com/directory1/cities/miami.php
should point to
www.example.com/directory1/miami
I'm using the following rules in my access file to change the url:
RewriteRule ^directory1/(alabama|alaska|arizona|arkansas|california|colorado|connecticut|delaware|florida|georgia|guam|hawaii|idaho|illinois|indiana|iowa|kansas|kentucky|louisiana|maine|maryland|massachusetts|michigan|minnesota|mississippi|missouri|montana|nationwide|nebraska|nevada|new_hampshire|new_jersey|new_mexico|new_york|north_carolina|north_dakota|ohio|oklahoma|oregon|pennsylvania|rhode_island|south_carolina|south_dakota|tennesee|texas|us_virgin_islands|utah|vermont|virginia|washington|west_virigina|wisconsin|wyoming)$ /directory1/states/$1.php [L]
RewriteRule ^directory1/(.*)$ /directory1/cities/$1.php [L]
However, nothing changes in the url bar and I always get a 404 not found. When I tested it with the htaccess checker, the output url is always correct. What is wrong with my rules? Is there a way to test how/if mod_rewrite is even functioning?
Some server configs require you to turn the rewrite engine on in your .htaccess file. Right at the top:
RewriteEngine on
I've also known some server configs where the file path starts/ends with a / (due to other rewrite rules already being run on the request) so perhaps allow for that to be there in the rules:
RewriteRule ^/?directory1/(alabama|alaska|arizona|arkansas|california|colorado|connecticut|delaware|florida|georgia|guam|hawaii|idaho|illinois|indiana|iowa|kansas|kentucky|louisiana|maine|maryland|massachusetts|michigan|minnesota|mississippi|missouri|montana|nationwide|nebraska|nevada|new_hampshire|new_jersey|new_mexico|new_york|north_carolina|north_dakota|ohio|oklahoma|oregon|pennsylvania|rhode_island|south_carolina|south_dakota|tennesee|texas|us_virgin_islands|utah|vermont|virginia|washington|west_virigina|wisconsin|wyoming)/?$ /directory1/states/$1.php [L]
RewriteRule ^/?directory1/(.*)/?$ /directory1/cities/$1.php [L]

.htaccess modRewrite - find out the underlying url that is trying to be resolved?

I am having trouble with an htaccess file that works on my local testing setup but not when uploaded to my production server, even though I have other sites on that server which have working .htaccess setups.
Is there any way (perhaps with a custom 404 page) that I can find out the url that .htaccess has re-written to, so that I can debug the problem?
what is your trouble?
get you an internal server error or didn't execute any of your rules?
a shoot in the dark ... if your rules doesn't match it could be that you have something like this in your .htaccess
RewriteRule /home/ /index.php?s=1&l=1 [L]
... then try the change in
RewriteRule home /index.php?s=1&l=1 [L]
without the leading slash "/"

Why is my .htaccess file redirecting to full server path instead of relative path?

I've never had a problem with cakePHP before, but something's odd about this server and is causing the redirects in the .htaccess files to behave oddly.
CakePHP uses mod_rewrite in .htaccess files to redirect requests to its own webroot folder. The problem is that the redirects are listing the wrong path and causing a 404 error. My CakePHP application, which is stored in the listings directory, has a .htaccess file as follows:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [R=301,L]
RewriteRule (.*) app/webroot/$1 [R=301,L]
</IfModule>
(*note that the R=301 causes an external redirect so we can see what is going on from our end. It should really omit this flag and do the redirect internally, transparent to end-users)
This is supposed to redirect any request from http://hostname.com/~username/listings/ to http://hostname.com/~username/listings/app/webroot/
However, rather than simply adding “app/webroot/” to the end as it is supposed to, it is adding the full server path ( /home/username/public_html/listings/app/webroot/ ) resulting in the final URL http://hostname.com/home/username/public_html/listings/app/webroot/ which is obviously incorrect and triggers a 404 error.
The hosting is on a shared hosting account, so that limits what I can do with the settings. I've never seen this happen before, and I'm thinking it's something wrong from the hosting side of things, but if anyone has some helpful suggestions then I can put them to the hosting company as well.
The solution to your question can be found towards the bottom of this page in the cakephp book:
For many hosting services (GoDaddy, 1and1), your web server is actually being served from a user directory that already uses mod_rewrite. If you are installing CakePHP into a user directory (http://example.com/~username/cakephp/), or any other URL structure that already utilizes mod_rewrite, you'll need to add RewriteBase statements to the .htaccess files CakePHP uses (/.htaccess, /app/.htaccess, /app/webroot/.htaccess).
I've deployed CakePHP from my profile's public_html folder as well. I had to change 3 the same .htaccess files mentioned above. Just add RewriteBase /~username/ to the .htaccess files just after RewriteEngine on!
Try removing .htaccess from main file... It worked for me
It was quite simple (using uolhost shared host):
Edit both .htaccess files:
/webroot/.htaccess
/.htaccess
Add the following line:
RewriteBase /
Here is the whole /webroot/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]