Hide the directory in the address bar of the browser - apache

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.

Related

How to rewrite url and redirect with apache mod rewrite?

I have got url:
ipaddress/panelname/main/index.php
How to rebuild it to
ipaddress/center/index.php
?
ofcourse we can see another pages, not only index.php, but this folders in url we can see forever.
I tryed to do this in .htaccess:
RewriteEngine on
RewriteRule ^center/([^/]+)/?$ panelname/main/$1 [L]
RewriteRule ^/panelname(.*)$ /center$1 [QSA,L,R=301,NC]
Redirect 301 ^/panelname(.*)$ /center$1
but i don't see redirect from panelname to center.
but if i type center all works good (but i don't shure, that it works good by my htaccess or by symlink, which i was created in filesystem)
How to rewrite all to another links and howto see redirect from old links to my new? Thank you.
RewriteRule in directory context (which .htaccess is), does never begin with a slash, because the common prefix is stripped from the matched portion first.
Redirect does match strings, not regex'es. The variant that works on a regex is RedirectMatch. Both only work on absolute URL's (the one beginning with a slash).
You either have to do the following:
RewriteRule ^panelname(.*)$ /center$1 [R,L]
or:
RedirectMatch 302 ^/panelname(.*)$ /center$1
Change [R] to [R=301] once you have tested that EVERYTHING works. If you choose the second option, only change 302 to 301 after testing that everything works.
If you want to show /center/index.php to your visitors and keep a redirect from old URL to this URL then you will need one redirect and one rewrite rule (that you already have).
RewriteEngine on
# external redirect from old URL to new one
RewriteCond %{THE_REQUEST} /panelname/main/(\S+) [NC]
RewriteRule ^ /center/%1 [R=302,L]
# internal forward from new URL to actual one
RewriteRule ^center/([^/]+)/?$ panelname/main/$1 [L]

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]

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

apache .htaccess file https:// redirect ---- rewrite it for subdirectory?

here is my code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
This will successfully rewrite everything to https (it will "force" https), if I put it in my home directory's .htaccess file. However, what about if I only want my subdirectory of /support to force https? How to rewrite the above code?
The intent is with regards to forcing https in WHMCS
Thanks in advance!
If you want to redirect only a subfolder and not a subdomain, that is even easier. To redirect the subfolder /support/ you would use the following code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^/support/(.*)$ https://%{HTTP_HOST}/support/$1 [QSA]
I added a QSA flag, which will force any query string that was entered to be appended to the redirect URL. If you don't use query strings in this section of the site, you can remove that flag. Also, if the string as shown doesn't work, you can try removing the leading slash from the RewriteRule and retrying it.
Add the following rule before the %{HTTPS} line:
RewriteCond %{HTTP_HOST} ^support.domain.com$
This will only allow the rule to execute if the visitor is coming from the subdomain. Please note, these rules will likely need to be put in the .htaccess file that is the root for the subdomain, not the main site root folder.

Location Bar Doesn't Show Redirected URL

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]