mod_rewrite and relative urls - apache

I'm setting up some simple url rewriting rules using mod_rewrite and a .htacces file, but I've got some problems.
If I set up the .htacces this way:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule /index.html /index.php [L]
when I call from the browser this url:
http://localhost/~dave/mySite/index.html
I got a 404 error.
Using this .htacces instead
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule /index.html http://localhost/~dave/mySite/index.php [L]
everything works fine and I get the index.php page I'm expecting.
Am I forced to user absolute urls as the target of a rewrite? Is this a Apache configuration problem?
I'm using Max OS X 10.6.2 with its standard Apache installation.

The RewriteBase base directive is setting a base path; you don't need to provide absolute paths in the redirection rules.
The rule pattern is not plain text; it's a regular expression.
If you set a full URL as target you instruct the browser to redirect to that other location; that kind of redirection is not transparent to the user.
To sum up, try this instead:
RewriteRule ^index\.html$ index.php [L]

Try doing
RewriteRule ^(.*)/index.html $1/index.php [L]
That should sort it.

Related

RewriteRule 301 redirect for whole directory and sub-directories

I really need your help with this one...
I'm simply trying to redirect EVERYTHING in a directory to another. It looks simple when I read about it, but in real life, it's not working... Here is my entire .htaccess file right now:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# Redirect all to HTTPS
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.org/$1 [R]
# End redirect
#301 REDIRECTS
Options +FollowSymLinks
RewriteRule ^mydir/(.*)$ /mydir-and-more/$1 [R=301,NC,L]
Fisrt, there is Wordpress stuff in didn't mess with.
Then, the code i copy/pasted from some site to redirect http to https. It works well. Note that i removed the "L" argument from the list to make sure my next rules will work.
After comes the part I'm strugling with.
So, it really is like that. My new directory starts with the same word then my old directory.
I copied this line from there: https://coolestguidesontheplanet.com/redirecting-a-web-folder-directory-to-another-in-htaccess/
On the Apache web site (https://httpd.apache.org/docs/current/mod/mod_rewrite.html) it says that i should use a / between ^ and mydir. Tried it, didn't work.
I tried moving Options +FollowSymLinks at the top of the file. Nothing.
When i use something like this:
RedirectMatch 301 ^/mydir/ https://example.org/mydir-and-more/
This works. But only moves the exact /mydir/ address. It doesn't move the whole directory. Also, if I type in https://example.org/mydir without the last /, it won't work. If i add the / in the Redirect match, it doesn't work anymore because its the same word!
So, here I am, totally confused! Please, any expert advise on this one? Thanks!!
You need to move you rules before the wordpress defined ones.
In fact if you try to access you site the rewrite rules elaboration stops at
RewriteRule . /index.php [L]
That instruction means "manage all paths that are not already beign managed by upper rule and stop elaboration ([L] stands for last)".
You can safely place your rules above the wordpress ones, better in a ifmodule
<IfModule mod_rewrite.c>
RewriteRule ^/mydir/(.*)$ /mydir-and-more/$1 [R=301,NC,L]
</IfModule>
# BEGIN WordPress
Funniest thing is, I solved my problem by fooling around! I didn't really need the RewriteRule, all I needed to write instead of the RewriteRule was exactly this :
Redirect 301 /mydir https://example.org/mydir-and-more
I don't even need the Options +FollowSymLinks.

htaccess 301 redirection of urls w/ parameters to url w/o parameters

I would like to do a simple 301 direct on some old URLs that have parameters to them.
e.g.
/dir/page1/?id=4590
/dir/page1/?id=45
/dir2/page5/?id=4590
/dir2/page9/?id=45
/page90/?id=45etc
...
I want a 301 redirect of the above urls to the corresponding urls w/o parameters:
/dir/page1
/dir2/page5
/dir2/page9
/page90
My experience with .htaccess is limited. What would be the simplest and the most correct way to enable the above redirects to actually work.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^id= [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]

Simple modrewrite to remove particular part of path

I'm trying to create a modrewrite rule that will change:
/blah/correct/xyz.htm
to
/correct/xyz.htm
There is not always /blah but when it's there, it always appears at the beginning of the URL. The URL can be any length, with numerous sub-paths. It could even be /blah/myfile.htm (which should just rewrite to /myfile.htm).
What's the best way to do this?
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule (?:^|/)blah(/.+)$ /$1 [L,NC]
This will internally forward /blah/foo to /foo or /blah/correct/foo to /correct/foo. If you want external rewrite then use:
RewriteRule (?:^|/)blah(/.+)$ /$1 [L,R=301,NC]

CakePHP 2.0.4 "URL rewriting is not properly configured on your server"

I've been trying to setup CakePHP on a development section of my server and I can't seem to solve the "URL rewriting is not properly configured on your server" error. I suspect I'm not configuring the .htaccess files with the correct RewriteBase. I've tried a wide variety of different RewriteBase for each file, but I can't seem to hit the right ones, and Cake doesn't give me any information other than "not working" (URL rewrite errors don't end up in Cake's error log).
I do not have access to my httpd.conf file, but I've used .htaccess and mod_rewrite with other frameworks (Wordpress and CodeIgniter) without a problem.
My base url for the site is: http://dev.domain.com/cake/
My base server path is: /home/username/public_html/dev/cake/
Cake root .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /cake
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
Cake app directory .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /cake/app
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
Cake webroot directory .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /cake/app/webroot
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
</IfModule>
Did you create a new default.ctp layout file and then "URL rewriting is not properly configured on your server." appeared?
If that is the case, it happened to me. It's working just fine. I think Cake is throwing a bad error message here.
Problem is with CSS.
If U don't make page on default layout of cake, and in your new layout You dont have default css: cake.generic.css it will show this error on page home.ctp. Look in code of home.ctp (View/Pages) and look - the id="url-rewriting-warning" of <p> where this error message is have in cake.generic.css value of display:none.
What does your vhost.conf look like? If you have a proper path in DocumentRoot, you shouldn't need to specify RewriteBase in .htaccess at all. Just stick with the defaults.
Try DocumentRoot /home/username/public_html/dev/cake/app in vhost, and remove RewriteBases from your .htaccess.
The problem for me was the css issue mentioned in another answer.
If you changed the css file in the default layout, you might need to add the following lines
to your new css file:
#url-rewriting-warning {
display:none;
}
... as long as rewriting appears to be working in all other ways.

URL Rewriting using .htaccess

To change the URL /mobiles.php?id=5 to /mobiles/5
The content of .htaccess file is as follows:
Options +FollowSymlinks
RewriteEngine on
RewriteRule /mobiles/$1 ^/mobiles.php?id=([0-9]+)$
But still it is showing /mobiles.php?id=5 in the address bar. Please help. Is there anything else needs to be added in the .htaccess file?
Note:
mod_rewrite module is enabled
I have restarted Apache server after making changes to the .htaccess
file
.htaccess file is in htdocs folder of Apache.
I am using Windows + PHP + Apache + MySQL
This works for me:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^mobiles/([0-9]+)$ mobiles.php?id=$1&rew [L]
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^mobiles.php$ /mobiles/%1? [R,L]
If you see this line:
RewriteRule ^mobiles/([0-9]+)$ mobiles.php?id=$1&rew [L]
I have added rew variable in the query string to prevent Apache to fall in an infinite loop
When Apache execute this line:
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
Is to make sure that url has not been rewritten for Apache
If your only concern is that the old url stays in the address bar, and you want this not to happen, try adding an [R] at the end.
RewriteRule ^/mobiles.php?id=([0-9]+)$ /mobiles/$1 [R]
Did you actually see the correct page?
By the way, the rewrite rules generally go the other way. I would be expecting to see something like:
RewriteRule ^/mobiles/([0-9]+)$ /mobiles.php?id=$1
Is your concern one of making sure a URL with query parameters does not show up in the address bar?
If I understand correctly, you want
Internally redirect /mobiles/5 to /mobiles.php?id=5
Also redirect the browser TO /mobiles/5 if a user navigates to /mobiles.php?id=5
For this you need 2 rules one to internally rewrite the URL for 1st case and 2nd for browser redirection.
You can do it like this:
RewriteEngine on
# for internal rewrite
RewriteRule ^/?mobiles/([0-9]+)/?$ /mobiles.php?id=$1 [L]
# for browser redirect
RewriteRule ^/?mobiles\.php\?id=([0-9]+)$ /mobiles/$1/ [R,L]
You are doing the opposite, should be:
RewriteRule ^/something/mobiles/([0-9]+)$ /something/mobiles.php?id=$1