Doing URL Rewriting and Redirections (.htaccess) with same code working both on WAMP localhost and remote folder? - .htaccess

first of all, I'm sorry for this umpteenth topic about this kind of problem.
I've tried to adapt other topics to my problem for months without being able to figure out how to do. That's why I'm writing today, it's really bugging me and as of today, I can only successfully do URL rewriting on remote but always coding in production remote folder isn't reliable solution.
This is what my file tree look like :
Firstly, I'd like to be redirected from the root of this 'template1' project to the root of the 'public' subfolder ; and secondly, I'd like to rewrite URLs so this kind of URL :
template1/index.php?route=qwertyuiop
turns into that :
template1/qwertyuiop
Both these things I want work but only on my remote folder on Ionos and I had to do weird things. For example for the redirection, I'm directly linking domain to subfolder and for URL rewriting I do :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.zip|\.pdf|\.css|\.svg|\.js)$
RewriteRule ^(.*)$ /index.php?route=$1 [L,QSA]
Anyways, this works fine on remote folders and when I do PHP header("location:/azertyuiop") it works fine but in localhost it tries to search at the root of WAMPserver and I don't know how to proceed to fix it while keeping same code for both local and remote.
If any of you could help me figure out how to deal with this problem, it would be really appreciated as reading topics and putting whatever I can in my .htaccess doesn't work on localhost.

Ok, sooo thanks #MrWhite for basically telling me about VirtualHost. I never saw about it in any topics so I was searching maybe in the wrong direction.
Everything is working fine, now. So in case in the future someone gets through it, I will explain what I did to make it work in localhost. What I will tell is translated from a website I've found in my native language ( How to configure VirtualHost (French) ).
STEP 1 : edit Apache's config in Wamp
In the 'httpd-vhosts.conf' file located in 'C:/wamp64/bin/apache/apache2.4.46/conf/extra/httpd-vhosts.conf' (in my case), I had to add these virtual host lines :
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.localhost
DocumentRoot "${INSTALL_DIR}/www/projets/projets_persos/tests/template1/public"
ServerName template1.local
ServerAlias template1.local
ErrorLog "logs/template1.local-error.log"
CustomLog "logs/template1.local-access.log" common
</VirtualHost>
Then I had to modify Apache's main config file called 'httpd.conf' located in 'C:/wamp/bin/apache/apache2.4.46/conf/httpd.conf' (in my case) and uncomment the following line :
#Include conf/extra/httpd-vhosts.conf (in my case, it was already uncommented)
After that, the website says to restart Wamp's services before step 2, which I did after step 2 below and it stills work without any trouble. So anyway, I guess it will be same for you, do it now or after step 2.
STEP 2 : edit Windows' hosts file
At bottom of the 'hosts' file located in 'C:/Windows/System32/drivers/etc/hosts' I added the following line at bottom of the file to override the previous rule :
127.0.0.1 template1.local
Aaand, that's all.
=-=-=-=-=
As a proof it's working well, here's a screenshot of my project :
Here you can see on the picture I exploded my route in an array. I do this for personal purposes, to be able to route according to whatever bit of string I want. If you want to get rid of the 'public' value in the route, just type $_GET['route'] = str_replace('public/', '', $_GET['route']); before putting each of its fields in the array. Then, it will look like this :
Also, of course, I used .htaccess to be able to do URL Rewriting, here's my .htaccess file :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.zip|\.pdf|\.css|\.svg|\.js)$
RewriteRule ^(.*)$ /index.php?route=$1 [L,QSA]
It's a bit off topic but in case you'd need to know, the RewriteCond lines allow to type in the true URL of assets that have these extensions and it won't rewrite as they are true URLs.
=-=-=-=-=
Anyway, this is the end of my solution, thanks again for helping me and all's well that ends well. The End.

Related

How do I remove a folder from a URL?

First of all, this question has been asked a few times on stack, however, none of the answers seem to work for me.
I have a website which has a "pages" folder in the root, I want to store all of my website pages in there.
Here's my structure
/pages/folder/folder2/index.php
I want to make it so the link displays:
https://wwww.website.com/folder/folder2/index.php
Removing the "/pages/" part of the URL, when I try all of the answers suggested previously, I get a 404 error.
Here is the htaccess I'm using
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^pages(/.*|)$ $1 [L,NC]
</IfModule>
and i also tried:
RewriteEngine On
RewriteRule ^pages/(.*)$ /$1 [L,R=301]
This htaccess is in the root. I can't seem to get it working, can anyone offer any suggestions? Thank you!
Your second attempt looks fine, though it can be imporoved a bit:
RewriteEngine On
RewriteRule ^/?pages/(.*)$ /$1 [R=301]
That rule should work inside the http servers host configuration or in some dynamic configuration file (".htaccess" style file) if the http server's rewriting module is loaded. You definitely should prefer the first option, but if you really need to use a dynamic configuration file then take care that the interpretation of such files is configured at all and that the file is placed in your hosts's DOCUMENT_ROOT folder and is readable for the http server process.
If that does not work then you need to start debugging. In this case you will start by monitoring your http server's error log file while making test requests. If no hint appears in there you need to enable rewrite logging which allows you to learn what exactly is going on inside the rewriting engine. See the official dpcumentation for the apache rewriting module for that. As typcial for OpenSource software it is of excellent quality and comes with great examples.

XAMPP URL with 'error' end to 404 Not found

Is there some problem with URLs which have word error in them? Because if I enter route like http://eshop.local/controller/action, where controller can be anything at all, there's no problem, but if controller is error, it returns:
Error 404 Page not found
My htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
http://eshop.local/error works fine but if is followed by some action whatever word, number, or anything returns 404 error.
EDIT:
Just now i have copied it to my webhosting, everything with error urls is working fine, so problem is on XAMPP side.
Hold Your Breath
Open xampp control panel
Stop Apache
Click the Config button on the Apache row, and select httpd.conf
In that file, search for <Directory "C:/xampp/htdocs">
A bit lower, you may see a line like this: # AllowOverride All. Remove the #, which is a comment
Save the file
Restart Apache, say a prayer, cross your fingers and hold your breath
Response from Altrea, Moderator from XAMPP forum:
All URLs that are used in an Alias, Location or Proxy definition.
There are a bunch of them in XAMPP Apache (depending on which config files are activated or not some of them are maybe not used):
/cgi-bin/
/error/
/examples
/icons/
/licenses
/manual
/my-gateway/
/phpmyadmin
/php-cgi/
/security
/server-info
/server-status
/uploads
/webalizer
/webdav
In your case /error/ is already useds
Is it possible to reuse for example that /error/ or it' s better to change it in my code for example to /err/ ?
This Alias is used for the default Apache
error Pages (everywhere ErrorDocuemnt is used this Alias is used).
So it is possible to change it in XAMPP, but you have to
search and replace multiple files.

The requested URL /public_html/ was not found on this server

I'm working on a local MAMP website. I use a micro MVC framework to use friendly urls
so I don't need to call index.php (which is inside of public_html directory) in the urls.
To achieve that, I have the following htaccess:
RewriteEngine on
RewriteCond %{REQUEST_URI} !public_html/
RewriteRule (.*) /public_html/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
Then I have the following line in /etc/hosts
127.0.0.1 mywebsite
Also, I have the following in httpd.conf
<VirtualHost *>
DocumentRoot "/Applications/MAMP/htdocs/mywebsite"
ServerName mywebsite
</VirtualHost>
So if I simply call http://mywebsite from the browser, the whole thing works smoothly.
So what's the problem?
The problem appears if I try to reach the same page from another machine in my LAN.
So if I write http://192.168.1.15/mywebsite the answer is:
Not Found
The requested URL /public_html/ was not found on this server.
I get the same message if I call http://localhost/mywebsite from my own machine.
I have the feeling that is something related to .htaccess, but I've been trying a
lot of different ideas I've found in the web, and nothing works.
I'd like to fix this, because I need other people to check the website from their machines.
If you have any clue please help. Thanks a lot.
Edit: I can't solve this, so as a temporary fix I've created a free account at AppFog for my team to be able to access the page until we go to production.
After all these years, I thought that computing would be easier... it's getting harder, actually. The htaccess file is a huge mistery to me! Thanks anyway :)
By the way... as I told you, I've found a way to fix the problem. The funny thing is I have to use a different .htaccess file. I thought you could be interested, provided that it seems you like computing stuff :)
This is the .htaccess that works in my LAN debian server:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/(www/.*)$
RewriteRule ^(.*)$ index.php [QSA,L]
As you can see, it's different from the one I showed you at my first post. My conclusion? Well, even when I always try to keep things simple, life shows me one and another time that everything can be more complicated. Look at the .htaccess file. It's so funny... I've learnt to work with it by using different combinations of code.
Isn't it crazy? Yes it is. Please let's do understandable software. Thank you!
Ok, I've been trying a lot of different things and all of them fail. The only solution I've found is to install the web site in a separated LAN debian server. Everything works ok in that way, but it's not possible to make it work from my machine with MAMP.
Why not? Well, I don't know. After a lot of years in computing, I've learnt to say "I donĀ“t know", you know what I mean.
I've been working for a few days with an AppFog account for free but, you know, that thing cannot work when you put a database and the whole thing there. Obvious.
So at the end the only solution I've found has been to put everything at that local LAN debian server.
Thank you anyway :)

htaccess rewrite working offline on WAMP but not online on linux host

I'm just going to explain my problem here :
http://mysite.com/movie/20000
should be rewritten to
http://mysite.com/movie.php?id=20000
my htaccess file :
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^movie/([0-9]+)$ movie.php?id=$1
On my localhost WAMP installation this works fine, but when I put it online on my linux host it doesn't completely work. It does go to the movie.php page, but it seems it gives no GET parameter id.
Edit :
if I use
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^movie([0-9]+)$ movie.php?id=$1
then
http://mysite.com/movie20000
Goes to the correct page, but I would like /movie/20000 and not just /movie20000
It also seems that my host automatically rewrites a visit to mysite.com/movie to mysite.com/movie.php
After searching for a long time, and pulling some of my lovely hair out I found the solution.
I just added
Options -MultiViews
to my htaccess file and that fixed it.
Why? Because .php was being added to urls without an extension, which I really did not need.
This should work.
RewriteRule ^movie/([0-9]+)$ http://mysite.com/movie.php?id=$1 [NC,L]
Don't forget the [NC, L] it means Case insensitive, last rule... If you don't, it will continue to process through your htaccess rules, maybe triggering other ones.
Still, the stuff below is good advice.... :)
Check to see if the Rewrite module is loading with apache. Look for this line in the httpd.conf file:
LoadModule rewrite_module modules/mod_rewrite.so
Check to see if your apache config allows for .htaccess files for your system or in the virtual host definition.
Also, test with a simpler rewrite catch all and test that alone to see if it's working at all like this (get rid of everything else in your htaccess file to limit the test):
RewriteEngine On
RewriteRule ^(.*)$ http://www.google.com [L,R=301]
Any request to your site should go to google if the configuration for apache is correctly set.

.htaccess - How to hide the internal directory used by subdomains?

I installed a small FTP space for my classmates which allows them to upload school related documents to my server. After the files have been uploaded, they can be easily accessed via a subdomain i.e. ftp.mydomain.com
Everything is working fine, the files can be downloaded and everything works properly.
There are two things which still annoy me though:
If you enter ftp.mydomain.com you see Index of /my_internal_directory instead of something like Index of /
Once you enter a sub-directory and click the Parent Directory link it will redirect you to ftp.mydomain.com/my_internal_directory/ as well
Is there any way to hide my_internal_directory ?
This is the content of my .htaccess file so far:
RewriteEngine on
RewriteCond %{HTTP_HOST} ftp\.mydomain\.com
RewriteCond %{REQUEST_URI} !^/my_internal_directory/
RewriteRule ^(.*)$ /my_internal_directory/$1 [L]
Any help would be greatly appreciated.
If you are using linux server then you can remove read permission for folder my_internal_directory
If not resolve, try to add:
Options -Indexes
on top of the file.
If still doesnt resolve please post your .htaccess file... :)

Resources