htaccess rewrites url on the address bar - apache

I have a blog (not a Wordpress one, just a very simple html/css blog) on a hosting provider.
I recently moved my blog from the root folder to an internal folder /www/dist.
Since the blog has many referral links on the web, I'd want the url to my blog posts stay the same, as if I never moved the blog to that folder.
Let's take as an example one of my posts: "forza 4 in c".
Before moving the blog, here it was the url to access the blog post:
https://www.gianlucaghettini.net/forza-4-in-c
Right now, to access the same blog post, I have to go to:
https://www.gianlucaghettini.net/www/dist/forza-4-in-c
I want to keep the url the same, so:
https://www.gianlucaghettini.net/forza-4-in-c
To achieve that, that's how I configured the htaccess file on the root. Basically I'm telling apache to redirect all the requests to /www/dist.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^gianlucaghettini\.net$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.gianlucaghettini\.net$
RewriteCond %{REQUEST_URI} !www/dist/
RewriteRule (.*) /www/dist/$1 [L]
I hoped that wouldn't change the address bar but that's not the case, I can see the address bar with the extra /www/dist part on the url.
Now, shouldn't the RewriteRule suppose to keep the url the same? Why I see such behavior?
UPDATE
I'm being told by the hosting team that the url update happens on my pc only. I tried to open the link from different clients, even from a different ip address but I still see the url being updated.
UPDATE no.2
It looks like everything works fine if I add a final slash / to all the links. In this case the url in the address bar is not changed.

Related

POST information getting lost in .htaccess redirect

So, I have a fully working CRUD. The problem is, because of my file structure, my URLs were looking something like https://localhost/myapp/resources/views/add-product.php but that looked too ugly, so after research and another post here, I was able to use a .htaccess file to make the links look like https://localhost/myapp/add-product (removing .php extension and the directories), and I'm also using it to enforce HTTPS. Now, most of the views are working fine, but my Mass Delete view uses POST information from a form on my index. After restructuring the code now that the redirect works, the Mass Delete view is receiving an empty array. If I remove the redirect and use the "ugly URLs" it works fine. Here's how my .htaccess file is looking like:
Options +FollowSymLinks +MultiViews
RewriteEngine On
RewriteBase /myapp/
RewriteRule ^resources/views/(.+)\.php$ $1 [L,NC,R=301]
RewriteCond %{DOCUMENT_ROOT}/myapp/resources/views/$1.php -f
RewriteRule ^(.+?)/?$ resources/views/$1.php [END]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I didn't actually write any of it, it's a mesh between answered questions and research. I did try to change the L flag to a P according to this post: Is it possible to redirect post data?, but that gave me the following error:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at admin#example.com to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
Apache/2.4.52 (Win64) OpenSSL/1.1.1m PHP/8.1.2 Server at localhost Port 443
POST information getting lost in .htaccess redirect
You shouldn't be redirecting the form submission in the first place. Ideally, you should be linking directly to the "pretty" URL in your form action. If you are unable to change the form action in the HTML then include an exception in your .htaccess redirect to exclude this particular URL from being redirected.
Redirecting the form submission is not really helping anyone here. Users and search engines can still see the "ugly" URL (it's in the HTML source) and you are doubling the form submission that hits your server (and doubling the user's bandwidth).
"Redirects" like this are only for when search engines have already indexed the "ugly" URL and/or is linked to by external third parties that you have no control over. This is in order to preserve SEO, just like when you change any URL structure. All internal "ugly" URLs should have already been converted to the "pretty" version. The "ugly" URLs are then never exposed to users or search engines.
So, using a 307 (temporary) or 308 (permanent) status code to get the browser to preserve the request method across the redirect should not be necessary in the first place. For redirects like this it is common to see an exception for POST requests (because the form submission shouldn't be redirected). Or only target GET requests. For example:
RewriteCond %{REQUEST_METHOD} GET
:
Changing this redirect to a 307/8 is a workaround, not a solution. And if this redirect is for SEO (as it only should be) then this should be a 308 (permanent), not a 307 (temporary).
Aside:
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Your HTTP to HTTPS redirect is in the wrong place. This needs to go as the first rule, or make sure you are redirecting to HTTPS in the current first rule and include this as the second rule, before the rewrite (to ensure you never get a double redirect).
By placing this rule last then any HTTP requests to /resources/views/<something>.php (or /<something>) will not be upgraded to HTTPS.

Rewriting directory into a query parameter in htaccess

I converted a website I'm building into a web view app in iOs.
I would like to track visitors that use the app instead of the website by adding a directory to my URL.
For instance, the "about" page URL would go from "https://example.com/about/" to "https://example.com/app-ios/about/"
My question is how to write an htaccess rule that tells my server to go to the path "/about/" and skip the "/app-ios/" directory?
Also, I'd like to add ?app=app-ios in my query parameters.
The most promising thing I found was this :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search/node/(.+)$ /search/node/?app=$1 [QSA,NC,L]
But I'd need to specify what comes before "foo".. In my case, "app-ios" is at the beginning of the request uri, always.
Plus I don't want a redirection. I just want my server to read /app-ios/something/other-thing/ as /something/other-thing/?app=ios.
First one question for tracking I would recommend to make this over a Query Parameter (as you wrote) like ?client=ios, but this might be an own opinion.
For rewriting the URL you could do the following:
to remove from the IOS from the URL (not tested):
RewriteEngine On
RewriteRule (.*)ios/(.*) $1/$2?client_id=ios&%{QUERY_STRING} [L]

Simple url masking via htaccess doesn't work

i'm trying to achieve the following:
I have domain.it and domain.fr
domain.it contains a website localized in multiple languages, so for example if you go to www.domain.it/fr/somecontent.php it shows that content in french language. This works. This website is hosted on a dedicated server.
In domain.fr i have an empty space with only an .htaccess. What i want is that when the user go to www.domain.fr/somecontent.php the browser shows the content from www.domain.it/fr/somecontent.php but keeping www.domain.fr/somecontent.php in the URL. So basically www.domain.fr/* should show the content from www.domain.it/fr/* but keeping www.domain.fr/* in the browser address bar.
Using an iframe is not an option because is not good for the SEO.
I'm using the following code inside the .htaccess on domain.fr (which is hosted on an OVH shared hosting):
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.fr$
RewriteRule ^(.*) https://www.domain.it/fr/$1 [P]
But when i open www.domain.fr or www.domain.fr/somecontent.php in the browser it says "Forbidden: You don't have permission to access /somecontent.php on this server."
Instead, if i place [L] or [L,R=302] in place of the [P] in the last line of .htaccess, it correctly redirect to the www.domain.it/fr/somecontent.php showing it contents, but it shows the destination url (the it domain) in the browser bar.
So i think the rules are correct, but for some reason when i use the [P] flag which as far as i know is needed to mask the url, it doesn't work.
Have you any clues ?
Thank you!

.htaccess point specific domain home to internal page

Okay...i am doing something kinda strange and out of the ordinary. I will try to explain...
I have several domain names pointed to the same root directory on my webserver. I can access the entire website using any of the domain names (I know this is bad for SEO, but there are reasons). Additionally, none of the "pages" really exist...as they are all being built dynamically...so i have some mod rewrite rules set up to point all incoming queries to view.php and passing in variables to generate the page content...anyway...
What i want to do is set ONLY THE HOME PAGE of two of my domains to (invisibly) use content of an existing sub page. Here is an example:
These all are the same page:
www.domain1.com/process/
www.domain2.com/process/
www.domain3.com/process/
which is really doing something like: view.php?page=process/
I want this page to display for www.domain2.com (and still also be accessible at domain2.com/process)
Essentially, there is a sub page of the site that i want to serve as the "HOME" page for domain2.com and domain3.com but if domain1.com should still use the default (index.php) HOME page.
I am sure i will need to post clarifications to this once replies start coming in...but here is what i have at the moment:
# special rules to set other domain names default homepage as specific internal page
RewriteCond %{HTTP_HOST} ^www.domain2.com [NC]
RewriteRule ^(.+)?$ /view.php?page=process/ [NC,L]
Currently this is sending ALL traffic to domain2.com to the "process/" page...so it is blocking out all other pages. I need to know how to have this rule ONLY apply to the base domain without any extra query string or url path. The key is that i DO NOT want to affect other pages within the domain...so i shoudl still be able to browse the whole site using this domain name...i just get started on a different view.
To only match the homepage, i.e. http://domain2com/ you need to match the empty path (as mod_Rewrite removes the leading /.
RewriteCond %{HTTP_HOST} ^www.domain2.com [NC]
RewriteRule ^$ /view.php?page=process/ [NC,L]

Get apache to serve different site to developers for same URL

The customers existing site is running nicely on the server from /home/nfc/public_html at the URL www.example.com.
We've developed a new site under /home/nfc2/public_html and this has been checked by using a development URL such as nfc2.dev.example.com.
But now I want to test for various links etc and check all works as expected.
Is there a way I can get Apache to serve me the new site if I browse to www.example.com but still show everyone else the old site.
It may be possible by using SetEnvIf REMOTE_ADDR type stuff to give me a specific site due to my IP address.
This may seem like not really needed - but the reason is that we have developed six sites at the same time and they all have many links between them - and currently those links go to the dev addresses so the customer could check all sites at the same time. I'd like to view the new sites on their new URL's so I can check all links have been altered.
You can just use a RewriteRule with the appropriate RewriteCond using mod_rewrite.
Example on remote_host:
RewriteEngine on
RewriteCond %{REMOTE_HOST} !^.+\.ourdomain\.com$
RewriteRule ^(/~.+) http://www.somewhere.com/$1 [R,L]
Or by address:
RewriteCond %{REMOTE_ADDR} ^123\.45\.67\.[8-9]$