Nginx and/or htaccess - .htaccess

By default (I'm using Xenforo for a forum), the URLs behave as:
https://< domain >/forums/< forumName >.< forumID >/
and
https://< domain >/threads/< threadName >.< threadID >/
What I'm attempting to do:
https://< domain >/< forumName >.f< forumID >/
and
https://< domain >/< threadName >.t< threadID >/
tl;dr: removes "forum" or "threads" from the URL & drops in a "f" or "t" prefix to the ID
Here's what I've been attempting (and failing) to put into place:
nginx.conf
rewrite ^/[^\./]+\.f([0-9]+)/$ /forums/$1/ permanent;
rewrite ^/[^\./]+\.t([0-9]+)/(.*)$ /threads/$1/$2 permanent;
.htaccess
RewriteRule ^[^\./]+\.f([0-9]+)/ /forums/$1/ [R=301,L]
RewriteRule ^[^\./]+\.t([0-9]+)/ /threads/$1/ [R=301,L]
full nginx.conf:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
location / {
rewrite ^/[^\./]+\.f([0-9]+)/$ /forums/$1/ permanent;
rewrite ^/[^\./]+\.t([0-9]+)/(.*)$ /threads/$1/$2 permanent;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
full .htaccess:
Code:
# Mod_security can interfere with uploading of content such as attachments. If you
# cannot attach files, remove the "#" from the lines below.
#<IfModule mod_security.c>
# SecFilterEngine Off
# SecFilterScanPOST Off
#</IfModule>
ErrorDocument 401 default
ErrorDocument 403 default
ErrorDocument 404 default
ErrorDocument 405 default
ErrorDocument 406 default
ErrorDocument 500 default
ErrorDocument 501 default
ErrorDocument 503 default
<IfModule mod_rewrite.c>
RewriteEngine On
# If you are having problems with the rewrite rules, remove the "#" from the
# line that begins "RewriteBase" below. You will also have to change the path
# of the rewrite to reflect the path to your XenForo installation.
#RewriteBase /xenforo
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
RewriteRule ^/[^\./]+\.f([0-9]+)/$ /forums/$1/ [R=301,L]
RewriteRule ^/[^\./]+\.t([0-9]+)/(.*)$ /threads/$1/$2 [R=301,L]
</IfModule>
Any idea on why these aren't behaving as expected?

Related

Migrating: Kohana .htaccess to nginx

I'm not a developer. I'm helping a friend to use a instance server at Amazon (Ubuntu) and I need a help to convert the .htaccess to nginx configs. I tried any sites that promise the automatic convertion, but without success. Can You help me in this case, please?
This is the .htaccess:
#http://# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
Thanks a lot
server {
listen 80;
server_name www.site.dev *.site.dev;
root /var/www/site/public_html/;
location / {
expires off;
try_files $uri $uri/ #kohana;
}
# Prevent access to hidden files
location ~ /\. {
deny all;
}
location #kohana {
rewrite ^/(.+)$ /index.php$request_uri last;
}
location ~* \.php {
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param KOHANA_ENV development;
fastcgi_cache off;
fastcgi_index index.php;
}
}
Source: https://github.com/primalskill/kohana-nginx-config

Migrate Kohana from Apache to NginX

I'm migrating a Kohana App version 3.0.4 from Apache to NginX.
I'm getting troubles with the configuration .htaccess. On NginX work correctly only the internal URL's like:
http://myurlwithoutwww.net.ds/internalpage/anotherpage
but nether the homepage
http://myurlwithoutwww.net.ds/
nether this:
http://myurlwithoutwww.net.ds/index.php
works. The last two will simply display a blank screen.
this is the htaccess file from where I'm trying to migrate:
AddType text/x-component .htc
#Accept-Encoding: filter,deflate
# Turn on URL rewriting
RewriteEngine On
AddDefaultCharset UTF-8
# Installation directory
RewriteBase /
RewriteCond %{HTTP_HOST} ^([a-z0-9\-]+).([a-z]{2,3})$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# ExpiresActive On
# <FilesMatch \.(bmp|png|gif|jpe?g|html?|ico|doc|swf)$>
# ExpiresDefault "access plus 10 days"
# </FilesMatch>
FileETag none
# Protect application and system files from being viewed
RewriteRule ^async/?(.*)? index.php?dispatcher=async&$1 [NC,L,QSA]
RewriteRule ^(?:_application|_modules|_system)\b index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php?kohana_uri=$0 [PT,L,QSA]
#RewriteRule .* index.php/$0 [PT]
And this my current Nginx configuration:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
include /etc/nginx/aliases.conf;
root /var/www/webroot/ROOT;
index index.php;
location / {
expires off;
try_files $uri $uri/ #kohana;
}
# Prevent access to hidden files
location ~ /\. {
deny all;
}
location #kohana {
rewrite ^/(.+)$ /index.php$request_uri last;
}
location ~* \.php {
#location ~ /\. { deny all; access_log off; log_not_found off; }
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /var/www/webroot/ROOT/$fastcgi_script_name;
fastcgi_param KOHANA_ENV development;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT /var/www/webroot/ROOT;
fastcgi_index index.php;
}
try below config, also check bootstrap init if it has 'index_file' => FALSE.
server {
listen 80;
server_name *.domain.com;
client_max_body_size 20M;
access_log /home/user/public_html/site/log/access.log;
error_log /home/user/public_html/site/log/error.log;
root /home/user/public_html/site/public/;
index index.php;
location / {
# don’t check $uri/, send to php for nice error message
try_files $uri /index.php?$query_string;
}
location = /index.php {
include fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
}
It is pretty old question, but maybe you will find it useful, the way I do:
location / {
index index.html index.htm index.php;
try_files $uri index.php;
}
location /kohana {
rewrite ^(.+)$ /kohana/index.php?kohana_uri=$1 last;
if (-f $request_filename) {
break;
}
if (!-e $request_filename) {
rewrite ^/kohana/(.+)$ /kohana/index.php?kohana_uri=$1 last;
}
}
I just listed the main sections of default.conf.

Convert .htaccess rules to nginx.conf

Hello I am trying to covert the following .htaccess lines so that arrowchat can work in my nginx server. The location of my arrowchat folder is like this /usr/share/nginx/html/arrowchat/ where /usr/share/nginx/html/ is the root folder for my website. My VPS has only one website, www.jukpac.com hosted on it. Below is the .htaccess code
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^chatroom ^/../public/chatroom/ [L]
RewriteRule ^cron ^/../public/cron/ [L]
RewriteRule ^debug ^/../public/debug/ [L]
RewriteRule ^list ^/../public/list/ [L]
RewriteRule ^mobile ^/../public/mobile/ [L]
RewriteRule ^popout ^/../public/popout/ [L]
RewriteRule ^video ^/../public/video/ [L]
</IfModule>
<IfModule mod_headers.c>
<FilesMatch "\.(gif|jpg|png|css|swf)$">
Header add "Expires" "Mon, 28 Jul 2014 23:30:00 GMT"
Header add "Cache-Control" "max-age=31536000"
</FilesMatch>
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A604800
ExpiresByType text/css A604800
ExpiresByType image/gif A604800
ExpiresByType image/png A604800
ExpiresByType image/jpeg A604800
ExpiresByType application/x-shockwave-flash A604800
</IfModule>
I tried a few online .htaccess converter but it didn't work can some one please let me know how to fix this?
My current default.conf file for nginx looks like this
#
# The default server
#
server {
listen 80;
server_name jukpac.com;
return 301 http://www.jukpac.com$request_uri;
}
server {
listen 80;
server_name www.jukpac.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
Ok with the Help of a friend, in Serverfault I managed to get the correct configuration as shown below
server { #redirecting server
listen 80;
server_name jukpac.com;
return 301 http://www.jukpac.com$request_uri;
}
server {
listen 80;
server_name www.jukpac.com;
root /usr/share/nginx/html; #move the root to server level
index index.php # move index to server level
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location / {
try_files $uri $uri/ =404;
}
location ~* \.(?:ico|css|js|gif|jpe?g|png|swf)$ {
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}

How to make Zend Framework 2 work with nginx?

I want to make Zend Framework 2 application to run within Nginx server. I could access the homepage, but I could not access any other modules. I found that Nginx needs a rewrite rule to access the URL such as domain/album. The question is how to convert the default .htaccess rules of Zend Framework 2 to Nginx rewrite rule?
Here are the .htaccess rules:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
You really do not need any rewrite rules to make Nginx and ZF2 to play nice together. Here is a very simple Nginx configuration which I use:
server {
listen *:80;
server_name DOMAIN;
# Character Set
charset utf-8;
# Logs
access_log /vhosts/DOMAIN/logs/access_log main;
error_log /vhosts/DOMAIN/logs/error_log;
# Directory Indexes
index index.php;
# Document Root
root /vhosts/DOMAIN/public;
# Location
location / {
try_files $uri $uri/ /index.php;
}
# Error Pages
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
# PHP-FPM Support
location ~ \.php$ {
fastcgi_pass unix:/usr/local/etc/php-fpm/DOMAIN.sock;
include fastcgi.conf;
}
# Block access to .htaccess
location ~ \.htaccess {
deny all;
}
}
Of course change the paths to your current setup. Since you did not mention what type of PHP installation you are using I can't help you there because I am currently using PHP-FPM.
Using this very simple setup all my modules are working as expected. For example I could visit http://example.com/some/url OR http://example.com/index.php/some/url
Nginx also has a simple configuration for ZF http://wiki.nginx.org/Zend_Framework#Time_for_nginx
Edit 1 - Added fastcgi_params config
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
To follow up on Diemuzi's answer.
Please remember, that if you want query parameters to be working in your application, you have to pass them along with try_files
Best way i found is to use both $is_args and $args. $is_args sets only a question mark if there is arguments, and $args pass the query arguments.
Replace
try_files $uri $uri/ /index.php;
with
try_files $uri $uri/ /index.php$is_args$args;
Now you can use query params as normally in your ZF2 application for example in a default controller
$query_params = $this->params()->fromQuery();

What should be nginx configuration while switching from htaccess?

I am using nginx for the first time in my life.
I came to know that nginx does not follow htaccess.
Rewrite rule the way we applied in htaccess should apply in nginx conf file. I don't know how.
I opened nginx conf file. I used this converter to convert my htaccess to nginx config file. I attached htaccess and equivalent config file.
I even don't know whether rewrite mod is enabled or not? Or I will say I don't know exact way to find it out.
htaccess code
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
php_value session.cookie_domain .abc.in
Nginx conf
user www-data;
worker_processes 4;
pid /var/run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
#rewrite on;
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##
#include /etc/nginx/naxsi_core.rules;
##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##
#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
server
{
listen 80;
server_name abc.in;
# nginx configuration
location /
{
if (!-e $request_filename)
{
rewrite ^(.*)$ /index.php;
}
}
}
}
I was wrong. This is a path mistake. I was changing file at different location.
I never found clear documentation on internet for this.
When I was doing some stuff on nginx server found that nginx does have sites-enabled directory which holds a file named default which holds server level configuration.
I changed index.html to index.php and everything works!!!

Resources