Why is rewrite not working to mask an URL? - .htaccess

I have a page with an url like this:
https://example.net:9000/something
I want to be able to access it from here https://example.net/mysite
and mask the url as well so it will always show everything like this before the last /
I tried like this:
RewriteRule ^:9000/something/?$ /mysite/
but it didnt work. Why?
also, I have some other rules on my htaccess that have to stay unaltered:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(jpeg|jpg|gif|png)$ /public/404.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L] <---everything up to this point has to stay the same
RewriteRule ^:9000/something/?$ /mysite/
</IfModule>
Thank you.

https:/example.net/ and https://example.net:9000 hare two separate http hosts that have nothing to do with each other. So you'd actually need the server for https:/example.net/ to proxy requests meant to be relayed to https://example.net:9000.
Assuming that https:/example.net/ is served by an apache http server (due to how you ask your question), this should be possible using the apache proxy module, either directly:
ProxyRequests off
ProxyPass /mysite/ https://example.com:9000/something/
ProxyPassReverse /mysite/ https://example.com:9000/something/
( Yes, indeed ProxyRequests off and not onif you implement this in the central server configuration. Leave that line away if you only have access to dsitributed configuration files (".htaccess") )
... or via the rewriting module:
RewriteEngine on
RewriteRule ^/?mysite/(.*)$ https://example.com:9000/something/$1 [P]
For that the proxy module and the proxy_http modules have to be loaded in the http server, obviously. The rule has to be implemented in the configuration of the http host serving https:/example.net/. If you do not have access to that you can instead implement it in the distributed configuration file (typically called ".htaccess") located in the DOCUMENT_ROOT of that host.

Related

htaccess URL masking subfolder to another domain instead of redirecting

I need to redirect www.domain.com/folder to subdomain.anotherdomain.com without change the first url, www.domain.com/folder. In www.domain.com I have a WordPress site and in subdomain.anotherdomain.com a Moodle site. By that, only www.domain.com/folder must have a redirect, not the www.domain.com or www.domain.com/other_folder. It can be done? It´s possible?.
In the Worpress site I´ve this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I try with a iframe, but a Google Chrome don´t like that :/ (cookies issues)
Many thanks in advance.
You cannot somehow keep a URL pointing to one http server visible in the browsers URL bar but make it connect to some other http server. That would be a gigantic security issue.
If you can change the setup such that both host names point to the same http server, then what you ask can be achieved by applying simple some internal rewriting rules.
For separate http servers your only option is to use the proxy module the apache http server brings, if it is installed and enabled ...
ProxyPass /folder https://subdomain.anotherdomain.com
ProxyPassReverse /folder https://subdomain.anotherdomain.com
See the documentation of the proxy module for details: http://httpd.apache.org/docs/current/mod/mod_proxy.html#proxypass
The proxy module can also be used from within the rewriting module, but this comes with an optional decrease of speed:
RewriteEngine on
RewriteRule ^/?folder/(.*)$ https://subdomain.anotherdomain.com/$1 [P,QSA,END]
Again this is documented in the rewriting module:
http://httpd.apache.org/docs/current/rewrite/flags.html#flag_p

Advence .htaccess redirect with one exception

I need help with .htaccess redirect.
There is a lot of simmilar answers, but did not find the one for my case.
I ve got this config:
<IfModule mod_rewrite.c>
RewriteEngine On
# Rewrite to index.php if not file or directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Now I need redirection for all url but not for 'news_list'
This work for all redirections:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ https://www.newsite.com/$1 [R=301,L]
</IfModule>
For exception I should use:
RewriteRule !^news_list($|/) https://www.newsite.com/$1 [R=301,L]
This works - 'news_list' do not redirect, but problem is, that than '/news_list' doesn't work (because it need point to 'index.php?url=news_list'.
So I need kinda mix of all configs above, but no luck.
Thank you in advance!
This probably is what you are looking for:
RewriteEngine On
# Rewrite everything to new host except for /news_list...
RewriteCond %{REQUEST_URI} !^/news_list(?:$|/)
RewriteRule ^ https://www.newsite.com/$1 [R=301,END]
# Rewrite to index.php if not file or directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)$ /index.php?url=$1 [QSA,END]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Redirecting to another domain with query strings

I am moving to another domain and I would like to redirect dynamic and static ULRs too. I have tried many .htaccess redirect rules but they don't work. For example:
FROM: http://www.bercsekft.hu/motorosbolt/index.php?id=546
TO: https://www.bercsemotor.hu/k/robogo-alkatreszek/aprilia-alkatreszek/aprilia-kormany-alkatresz
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/index.php$
RewriteCond %{QUERY_STRING} ^id=509$
RewriteRule ^(.*)$ https://www.bercsemotor.hu/k/robogo-alkatreszek/aprilia-alkatreszek/aprilia-kormany-alkatresz [R=301,L]
I have also another rules in .htaccess. I am not sure if they have affect to the redirect rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^bercsekft.hu$
RewriteRule ^(.*)$ "http\:\/\/www\.bercsekft\.hu\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^bercsekft\.hu$ [OR]
RewriteCond %{HTTP_HOST} ^www\.bercsekft\.hu$
RewriteRule ^jarmu\/?$ "http\:\/\/www\.bercsekft\.hu\/motorosbolt" [R=301,L]
RewriteCond %{HTTP_HOST} ^bercsekft\.hu$ [OR]
RewriteCond %{HTTP_HOST} ^www\.bercsekft\.hu$
RewriteRule ^edeny\/?$ "http\:\/\/konyhafelszereles\.bercsekft\.hu\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^bercsekft\.hu$ [OR]
RewriteCond %{HTTP_HOST} ^www\.bercsekft\.hu$
RewriteRule ^edenybolt\/?$ "http\:\/\/konyhafelszereles\.bercsekft\.hu\/" [R=301,L]
RewriteBase /
RewriteRule ^sitemap.xml$ motorosbolt/sitemap2.php [L]
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php52” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php52___lsphp .php .php5 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
Loos like your issue is the location of that dynamic configuration file you use (".htaccess") in combination with the paths you defined in there. If you really need to use a dynamic configuration file then it usually is easiest to place it in the http servers DOCUMENT_ROOT folder. You need to make sure that such files are considered at all by the http server (hint: the AllowOverride directive) and that the file is readable by the http server process.
This is a slightly modified version of your code to adjust the paths, also I replaced the numerical ID, your question contradicts itself in that detail:
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)id=546(&|$)
RewriteRule ^/?motorosbolt/index\.php/?$ https://www.bercsemotor.hu/k/robogo-alkatreszek/aprilia-alkatreszek/aprilia-kormany-alkatresz [R=301]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Redirect rewritten URL to a new rewritten URL

I'm working on my website link architecture. I would like to redirect with my .htaccess already rewritten URL to a new one to keep my incoming links active.
www.website.com/profile-info/ to www.website.com/profile/
This is my actual working .htaccess :
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_URI} /profile-info/?$ [NC]
RewriteRule . models.php [L]
So in order to redirect /profile-info/ to the new URL : /profile/ . I end up with this code. However it redirects /profile/ to /profile-info/.
RewriteCond %{REQUEST_URI} /profile/?$ [NC]
RewriteRule . /profile-info/ [L,R=302]
I think this is what you are looking for:
RewriteEngine On
RewriteMap /
RewriteRule ^/?profile-info/?$ /profile [END,NE,R=301,QSA]
RewriteRule ^/?profile/?$ models.php [L,QSA]
It is a two step strategy:
redirect "old" links pointing to /profile-info to the new /profile
internally rewrite /profile to models.php
You may have to change the RewriteMap, this obviously depends on your situation.
And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using .htaccess style files. Those files are notoriously error prone, hard to debug and they really slow down the server, often without reason. They are only provided for situation where you do not have access to the host configuration (read: really cheap hosting providers) or in case an application needs to write its own rules (which is an obvious security nightmare...).
You can use these 2 rules:
Options +FollowSymlinks -MultiViews
RewriteEngine On
# redirect /profile-info to /profile
RewriteRule ^profile-info/?$ /profile [R=301,NE,L]
# rewrite /profile to /models.php
RewriteRule ^/?profile/?$ models.php [L,NC]

htaccess : how to transform the url hello/ola/ciao/register.php to u/u/u/page.php?

I have read a some post and apache documentation on this topic.
I want to transform the url hello/ola/ciao/page.php to u/u/u/page.php to hide the website logic.
If I write:
RewriteRule ^hello/ola/ciao/(.*)$ u/u/u/$1 [R]
or
RewriteRule ^hello/ola/ciao/(.*)$ u/u/u/$1 [P]
it returns the message:
The requested URL /u/u/u/page.php was not found on this server
MY HTTPD.CONF is :
<Directory "C:/workspace">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Therefore I do not want redirect to u/u/u/page.php because u/u/u/ folders do not exist on the server.
I only want to transform the url to u/u/u/page.php, make it nicer.
Is it possible to obtain this result?
Is it possible rewrite url without redirect it?
I must to use .htaccess,RewriteRule P flag,ProxyPassReverse or PHP script?
What apache modules must to be enabled?
Which folder it needs to be put the .htaccess? In the root folder or
subfolder too?
It works in hoster web space or on localhost too ?
Try this rule in root directory
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^u/u/u/(.*).php$ hello/ola/ciao/$1.php [L]

Resources