Location Bar Doesn't Show Redirected URL - apache

This is my httpd.conf file for my http://old.example.com :
RewriteEngine on
RewriteBase /
RewriteRule ^login$ http://another.example.com/login [L]
The problem is that although I can redirect to http://another.example.com/login, but the location bar is still showing http://old.example.com/login.
Any idea how to fix this?

If you’re using mod_rewrite in the server or virtual host configuration, you always need to specify the full URL path in your patterns:
RewriteRule ^/login$ http://another.example.com/login [L,R]
Only when used in the per-directory context like in a .htaccess file you just need to specify the relative URL path without the contextual path prefix.
And also try an explicit redirect by setting the R flag:
RewriteRule ^/login$ http://another.example.com/login [L,R]
With that you can also specify a status code:
RewriteRule ^/login$ http://another.example.com/login [L,R=301]

Related

redirect based on directory in path via .htaccess

Via .htaccess, how do you redirect this
http://www.somedomain.com/de/foo
to this:
http://www.de-domain.com/foo
The redirection should depend on the second parameter, in the example above "de".
Depending on the server configuration you should be able to do this on the .htaccess for somedomain.com. If this works appropriately you might try [R=301,L] to make the redirection permanent:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?somedomain.com$
RewriteRule ^([a-z]{2})/(.*) http://www.$1-domain.com/$2 [R,L]

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.

mod_rewrite - simple redirect to parent directory

What I need is to redirect everything from a certain directory to the index file of the parent directory. The URL in the browser has to change as well, just straight redurect. How do I do this? I tried variations of
RewriteRule ^(.*)/?$ ../ [R]
but it displays an error and the actual system path!
Most easily accomplished by using an absolute http URL:
# We assume this is in .htaccess inside the directory to be redirected
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/parent
RewriteRule ^(.*)?$ http://%{HTTP_HOST}/parent [R=301,QSA,L]
Note, QSA was added to preserve the query string, but that can be removed if you don't want it.
You can use redirectMatch:
RedirectMatch 301 ^/child/?$ http://example.com/

I Cannot get rewriterule to work

I'm using VertrigoServ 2.27 on my laptop (localhost:8080).
Rewrite module is enabled and i tested it with alice.html and bob.html example
(http://stackoverflow.com/questions/6944521/url-rewriting-on-localhost)
and it works with .htacces inside www-subfolder. And I also put rubbish text inside .htaccess and
I got error from Apcheserver so rewrite mod is running and I cab use rules.
here is the rule: (inside /www/folder1/.htaccess)
Options +FollowSymLinks
RewriteEngine On
RewriteRule /(.*) /index.php?disp=$1 [QSA,L]
So when I put this url into browser my index page loaded ok.
http://localhost:8080/folder1/index.php
And the problem is here: When I request login via index.page(login page) and send url to localhost
server by cliking send-button
the url changed localhost:8080/login, it should be localhost:8080/folder1/login
how I can keep subfolder name in url?
and I want convert urls like this: www.best-food-of-the-usa.com/index.php?operation=top&state=arizona&
city=mesa&limit=10
to like this: www.best-food-of-the-usa.com/arizona/mesa/top10.html
Any help is appreciated. Thanks
\Jose
RewriteRule is behaving as the docs say.
From RewriteRule Directive Apache Docs
What is matched?
In VirtualHost context, The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html").
In Directory and htaccess context, the Pattern will initially be matched against the filesystem path, after removing the prefix that lead the server to the current RewriteRule (e.g. "app1/index.html" or "index.html" depending on where the directives are defined).
If you wish to match against the hostname, port, or query string, use a RewriteCond with the %{HTTP_HOST}, %{SERVER_PORT}, or %{QUERY_STRING} variables respectively.
So, when in Directory and htaccess context the prefix /www/folder1/ will be removed. Also remember when matching with RewriteRule in Directory and htaccess context, the pattern will never begin with /.
So, your RewriteRule Should be:
Options +FollowSymLinks
RewriteEngine On
RewriteRule (.*) /index.php?disp=$1 [QSA,L]

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