I'm new to apache's mod_rewrite and i'm having a problem ...
If i use the url : mysite.com/nl/ or mysite.com/fr/ i'm getting redirected to the right page, when a user enters the url mysite.com i redirect my user to the nl language but the problem is in the adress bar the url stays mysite.com and i would like it to be mysite.com/nl
this is my htaccess file:
RewriteEngine on
DirectoryIndex index.php
RewriteBase /
# Rewrite voor taal en pagina
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ $1/$2 [R]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?tl=$1&pg=$2 [L]
#rewrite voor taal
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ $1 [R]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?tl=$1 [L]
my folder structure is:
[nl]
index.php
contact.php
[fr]
index.php
contact.php
index.php
the content of index.php (in the root directory):
<?
switch ($_REQUEST['tl'])
{
case 'nl':
switch ($_REQUEST['pg'])
{
case 'contact':
include_once("nl/contact.php");
break;
default:
include_once("nl/index.php");
break;
}
break;
case 'fr':
include_once("fr/index.php");
break;
default:
include_once("nl/index.php");
break;
}
?>
So when a users enters mysite.com it shows the index.php from the nl directory but the url in the browser address stays mysite.com but should be mysite.com/nl/
Since i have zero experience in this rewriting rules i've been stuck at it for hours ...
Thanks in advance,
Davidi
Instead of using include_once try using header('Location: /nl/page.php') or header('Location: /fr/page.php');
Related
can anybody help me to convert .htacess file to nginx rewrite rule
my htaccess blew
RewriteEngine On
RewriteRule ^video/([^/]*)/([^/]*)\/$ /index.php?page=details&title=$1&id=$2 [L]
RewriteRule ^video/([^/]*)\.html$ /index.php?page=result&q=$1 [L]
i try apply on here:
/etc/nginx/sites-available/default
# nginx configuration
location /video {
rewrite ^/video/([^/]*)/([^/]*)\/$ /index.php?page=details&title=$1&id=$2 break;
rewrite ^/video/([^/]*)\.html$ /index.php?page=result&q=$1 break;
}
not working..
If the link does not have any value, redirect to index.php.
Example;
www.mydomain.com/ --> index.php
If the link is www.example.com/post/value, redirect to post.php.
Example;
www.mydomain.com/post/cDfS58Q --> post.php
If the link only consists of value, redirect to profile.php.
Example;
www.mydomain.com/jhon.34 --> profile.php
My test:
RewriteRule ^post/([0-9a-zA-Z-_/.]+)$ post.php?$1 [QSA,L]
RewriteRule ^([0-9a-zA-Z-_/.]+)$ profile.php?$1 [QSA,L]
but they all go to post.php.
Set the rewrite rules for each scenario except for the link that does not have any value. The default file that opens when visiting a website is the index file so you don't need to explicitly set it.
Go from longest URI to smallest because the condition of the smaller URI usually satisfies the conditions for the bigger URI.
Try somethig like this:
# Mod Rewrite setup
Options +FollowSymlinks
RewriteEngine on
AddDefaultCharset UTF-8
# First enter the one with the longest URI
RewriteCond %{REQUEST_URI} ^/?post/([0-9a-zA-Z-_/.]+)/?$
RewriteRule ^(.*)$ /post.php?data=%1 [NC,L]
# Then the one with only value
RewriteCond %{REQUEST_URI} ^/?([0-9a-zA-Z-_/.]+)/?$
RewriteRule ^(.*)$ /profile.php?data=%1 [NC,L]
I did it in a different way ;
htaccess
RewriteRule ^p/(.*)$ post.php?$1 [QSA,L]
RewriteRule ^([0-9a-zA-Z]+)$ index.php?$1 [QSA]
RewriteRule ^([0-9a-zA-Z]+)/$ index.php?$1 [QSA]
index.php
<?php
if (!empty($_SERVER['QUERY_STRING'])) {
//Profile.php will be included here.
echo "You are in profile now.";
}else {
//Homepage.php will be included here.
echo "You are in profile now.";
}
?>
Url : www.localhost.com/adsasd
Output : You are in profile now.
Url : www.localhost.com/
Output : You are in homepage now.
I have a simple home page (index.html) which has a hyperlink to a subfolder named as main which contains index.html. You can witness the same at: rajiviyer.in
Code for hyperlink:
<p>Go to sub folder page - Main</p>
How to configure .htaccess for this so, that I can directly access http://site_name/main/*.html?
You can do this with PHP:
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.domain.de/main");
header("Connection: close");
?>
Or, like you actually want it, with a .htaccess entry:
RewriteEngine On
Redirect 301 / http://www.domain.de/main
Given your comment, please try the below instead:
Options +FollowSymLinks -Multiviews
# Set default page for a directory to index.php
DirectoryIndex index.php index.html
# Turn on the rewrite engine for this override
RewriteEngine On
# Force www.
RewriteCond %{HTTP_HOST} ^rajiviyer\.in [NC]
RewriteRule ^(.*)$ http://www.rajiviyer.in/$1 [R=301,L]
# Redirect site root to main directory
RewriteRule ^$ /main/ [R=302,L]
Once you're happy, change 302 to 301 to make the redirect cached by browsers and search engines.
I have moved site from apache to nginx and I have got some issue with nginx.
htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(js|css|png|jpg|jpeg|bmp|ttf|php|php5|html|htm|gif)$
RewriteRule ^(.+)/(.+[^/])/(.*[^/])/?$ index.php?p=$1&subp=$2&subsubp=$3 [QSA,L]
RewriteCond %{REQUEST_URI} !\.(js|css|png|jpg|jpeg|bmp|ttf|php|php5|html|htm|gif)$
RewriteRule ^(.+)/(.+[^/])/?$ index.php?p=$1&subp=$2 [QSA,L]
RewriteCond %{REQUEST_URI} !\.(js|css|png|jpg|jpeg|bmp|ttf|php|php5|html|htm|gif)$
RewriteRule ^(.*[^/])/?$ index.php?p=$1 [QSA,L]
Options -Indexes
And using converter to nginx:
location ~ \.(js|css|png|jpg|jpeg|bmp|ttf|php|php5|html|htm|gif)$ {
}
location ~ \.(js|css|png|jpg|jpeg|bmp|ttf|php|php5|html|htm|gif)$ {
}
location ~ \.(js|css|png|jpg|jpeg|bmp|ttf|php|php5|html|htm|gif)$ {
}
autoindex off;
location / {
rewrite ^/(.+)/(.+[^/])/(.*[^/])/?$ /index.php?p=$1&subp=$2&subsubp=$3 break;
rewrite ^/(.+)/(.+[^/])/?$ /index.php?p=$1&subp=$2 break;
rewrite ^/(.*[^/])/?$ /index.php?p=$1 break;
}
I think that, can work when we are not talking about subdomain.
Yes - my site is in subdomain called /mysub/
So to enter my website I use mysub.domain.com or domain.com/mysub/
(where the first is this:)
if ($http_host = "mysub.domain.com") {
rewrite ^(?!/\b(mysub|stats|error)\b)/(.*)$ /mysub/$2 ;
}
Question:
How to link these rules into one ?
I need to redirect the url http://testing.domain.com/ to http://testing.domain.com/admin/index.php
I think you are trying to secure your home page. Put this php code in your index.php.
header("Location: http://testing.domain.com/admin/index.php")
exit;
If you got everything in admin folder, then you can use this one
Options -Indexes
RewriteEngine on
RewriteCond $1 !^(admin)
RewriteRule ^(.*)$ admin/index.php [L]
Put the following into your .htaccess file
Redirect 301 http://testing.domain.com/ http://testing.domain.com/admin/index.php