Raspberry Pi web server remove .html/.php from URL - .htaccess

I'm using my Raspberry Pi as an webserver with a domain that i bought on strato.de with a dyndns. I attached a file named .htacces to my project with following code.
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
I also tried this one:
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule (.*) $1.html [L]
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.htm -f
RewriteRule (.*) $1.htm [L]
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
Before hosting my website on the webspace from STRATO.de everything worked with the first version, which you can see above. But not its only giving me an 404.
I just want to remove the ending .html/.php and other from my URL.

Log in into pi
sudo nano /etc/apache2/sites-enabled/000-default
Add:
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
# AllowOverride None
Order allow,deny
allow from all
</Directory>
sudo a2enmod rewrite
sudo /etc/init.d/apache2 restart oder service apache2 restart

Related

Redirect to other location

I have the current setup where my file system looks like this
/var/www/blog.example.com/v1/project.php
To access project.php you go to blog.example.com/project, now I've also added a GET which is a title.
Instead of going to blog.example.com/project?title=This Title I want it to be something like blog.example.com/project/This Title
How may I accomplish that setup? Current .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^project/(.*)$ v1/project.php?title=$1 [L,QSA]
RewriteCond %{REQUEST_URI} !/(v1)/ [NC]
RewriteRule ^(.*)$ %{ENV:BASE}v1/$1 [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
Have it like this:
# turn off MultiViews to avoid conflicts
Options +FollowSymLinks -MultiViews
RewriteEngine On
# handle /project/abcd URI
RewriteRule ^project/([^/]+)/?$ v1/project.php?title=$1 [L,QSA,NC]
# add .php extension intrnally
RewriteCond %{DOCUMENT_ROOT}/v1/$1\.php -f
RewriteRule ^(.+?)/?$ v1/$1.php [L]
# forward everything to v1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!v1/)(.*)$ v1/$1 [L,NC]
Try it like this,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([\w-]+)$ v1/$1.php
RewriteCond %{REQUEST_FILENAME}.php !-f
RewriteCond %{REQUEST_FILENAME}.php !-d
RewriteRule ^project/([\w-]+)$ v1/project.php?title=$1 [QSA,L]

htaccess will not work as supposed to for removing directory and removing parts of url

I cannot get the profile.php?username= to go away for some reason. I did the exact same thing in another folder and it is working but won't work on this folder and I don't see my mistake.
The other problem, I want files inside my pages directory to be displayed like: localhost/test_a/filename (while the true location is localhost/test_a/pages/filename) but I don't know how to internally redirect to it
RewriteEngine On
RewriteBase /
# remove /index.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ %1 [R=301,L]
# externally redirect /dir/foo to /foo
RewriteCond %{THE_REQUEST} pages/
RewriteRule pages/(.*)$ test_a/$1 [L,NC,R]
# internally forward /foo to /dir/foo
--------------------------------------------------------------what goes here?
# externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
# internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/?$ test_a/$1.php [L]
# Remove long profile url to simple 'profile/username'
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profile/(.*)$ test_a/profile.php?username=$1
-------------------------------------------------------why is this not working?
#Deny access to htaccess
<files .htaccess>
order allow,deny
deny from all
</files>
Try turning multiviews off. In some installations, multiviews is turned on which tells mod_negotiation to try and "guess" a request filename. In this case, /profile/asdasds is being guessed to be /profile.php/asdasds and that will bypass your mod_rewrite rule entirely.
So add this to the top:
Options -Multiviews
As for the first question, try this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/test_a/pages/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/test_a/pages/$1 -d
RewriteRule ^(.*)$ test_a/pages/$1 [L]

.htaccess files not working on apache

/etc/apache2/sites-available/default.conf reads:
<Directory>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
</Directory>
I have mod_rewrite enabled
Under /var/www/sitename
I have my .htaccess file which reads:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Yet, when access localhost/sitename/directory I get a 404 error
What am I missing here?
Try adding RewriteRule ^(directory)($|/) - [L] above the first RewriteCond so it will exclude the directory from RewriteRule you can add more than 1 directory by seperating them with | like RewriteRule ^(forum|extras)($|/) - [L]
Here is an example:
RewriteEngine On
RewriteBase /
RewriteRule ^(directory)($|/) - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
For people using ubuntu I found this answer to be complete.
https://askubuntu.com/a/48363
try this -
Options +FollowSymLinks
RewriteEngine on
RewriteRule process-id-(.*)\.htm$ process.php?id=$1

Add another exception to htaccess?

My .htaccess file alows access to real files & folders on the server. For anything else, it will redirect to /index.php, except when the app folder is specified in which case it will redirect to /app/index.php. I want to add a second folder named appbeta which will redirect to /appbeta/index.php. How should I modify my .htaccess to add this additional exception?
SetEnv APPLICATION_ENV development
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!app)(.+)$ /index.php?u=$1 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^app(.+)$ /app/index.php?u=$1 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
I am not exactly sure what the following rewrite does:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!app)(.+)$ /index.php?u=$1 [NC,QSA,L]
However, if your current .htaccess is working as you want, I would try the following which puts a rewrite for /appbeta right before the rewrite for /app:
SetEnv APPLICATION_ENV development
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!app)(.+)$ /index.php?u=$1 [NC,QSA,L]
# rewrite /addbeta to /appbeta/index.php ...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^appbeta(.*)$ /appbeta/index.php?u=$1 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^app(.+)$ /app/index.php?u=$1 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
note: no new question started as mentioned in comments, fix above (change ^appbeta(.+)$ to ^appbeta(.*)$)

htaccess configure rewrite rules for local, wip domain and prodution domain in the same file

I need to configure my htaccess with rules for local developemnt, wip domain and production domain, so i dont need to edit when I test the project in these domains.
I think I am close, but its not working.
RewriteEngine On
#PRODUCTION
RewriteCond %{HTTP_HOST} !^(www\.)?PRODUCTION_DOMAIN\.com$
RewriteRule ^ - [S=3]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
#WIP
RewriteCond %{HTTP_HOST} !^(www\.)?WIP_DOMAIN\.org$
RewriteRule ^ - [S=3]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wip/PROJECT_NAME/index.php [L]
#LOCAL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /PROJECT_NAME/index.php [L]
Thank you.
I used to run my apache server with -DLOCALDEV flag and then my .htaccess looks like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
<IfDefine LOCALDEV>
SetEnv APPLICATION_ENV development
// any local specific goes here
</IfDefine>

Resources