I have the below htaccess file in my website and it was working perfectly for last two years. Recently i migrated the server to a new hosting partner and seems it not working as expected. I have confirmed that the server is supporting mod_rewrite module. And i could see the [QUERY_STRING] => is null what ever URL i specify, and all the URLs are routing to the home page. Can any one tell what i need to modify. i saw a lot many answers in stackoverflow and nothing worked for me. I am just a begginer in this htaccess file.
Options +FollowSymLinks
RewriteEngine On
Options -Indexes
# REWRITING index.php AS index #
RewriteRule ^index/?$ index.php [NC]
# Route The Pages #
RewriteRule ^index/([{a-z}]+)/?$ index.php?$1 [NC,L]
RewriteRule ^index/home/([0-9]+)/?$ index.php?home&p_id=$1 [NC,L]
RewriteRule ^index/page/([{a-z}{\-\}{0-9}]+)/?$ index.php?page&show=$1 [NC,L]
RewriteRule ^index/gallery/([{image}{video}]+)/?$ index.php?gallery&type=$1 [NC,L]
RewriteRule ^index/gallery/([{image}{video}]+)/([0-9]+)/?$ index.php?gallery&type=$1&album=$2 [NC,L]
If you are matching video or image then there is no reason to have {video} as { and }will match literally{video}`.
Have your .htaccess this way:
Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine On
# REWRITING index.php AS index #
RewriteRule ^index/?$ index.php [NC,L]
# Route The Pages #
RewriteRule ^index/([a-z]+)/?$ index.php?$1 [NC,L,QSA]
RewriteRule ^index/home/([0-9]+)/?$ index.php?home&p_id=$1 [NC,L,QSA]
RewriteRule ^index/page/([a-z0-9-]+)/?$ index.php?page&show=$1 [NC,L,QSA]
RewriteRule ^index/gallery/(image|video)/?$ index.php?gallery&type=$1 [NC,L,QSA]
RewriteRule ^index/gallery/(image|video)/([0-9]+)/?$ index.php?gallery&type=$1&album=$2 [NC,L,QSA]
Options +FollowSymLinks
RewriteEngine On
Options -Indexes
The empty query string is consistent with MultiViews being enabled (perhaps as a result of a server update). Try disabling MultiViews at the top of your .htaccess file:
Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine On
If MultiViews is enabled then a request for /index/<something> would result in an internal subrequest to /index.php/<something> and none of your remaining directives will match.
However, you do still need to update your regex to something like what anubhava suggests, since your current regex is probably matching a lot more than you intend. But your current patterns are ambiguous. For example, what should [{a-z}{\-\}{0-9}]+ match? It looks like you perhaps intended it to be a <letter>-<digit>? However, it currently matches any combination of letters, digits and hyphens (which is how anubhava has interpreted it)?
Related
My .htaccess doesn't work with GET. It displays "news/", but once I go "news/1" or "news/1/" it doesn't work.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^/?$ index.php [NC,L]
RewriteRule ^news/$ news.php
RewriteRule ^news$ news.php
RewriteRule ^news/([a-zA-Z0-9_-]+)/$ news.php?id=$1 [L,QSA]
RewriteRule ^news/([a-zA-Z0-9_-]+)$ news.php?id=$1 [L,QSA]
My PHP is:
if(!isset($_GET["id"])){
$Article->printArticles();
}else{
$Article->printArticle($_GET["id"]);
}
But it doesn't find the $_GET["id"] but at localhost it works.
Have tried with [L,QSA], [N], [NC,L] and without any of these.
How shall the htaccess look to find the $_GET["id"]?
How shall the htaccess look to find the $_GET["id"]
This is most likely result of MultiViews option being ON on your web server.
Turn it off using this line at the top:
Options +FollowSymLinks -MultiViews
Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So if /news is the URL then Apache will serve /news.php.
I ask your help because I would like to do soms URL rewritting.
Here is the htacess file I wrote for my site
# Do not remove this line or mod_rewrite rules and search engine friendly URLs will stop working
RewriteBase /
Options +FollowSymlinks
Options -Indexes
php_flag display_errors off
RewriteEngine on
RewriteRule ^helper/?$ index.php?action=helper [NC,L]
RewriteRule ^probabilite/?$ index.php?action=proba [NC,L]
RewriteRule ^demon/?$ index.php?action=demon [NC,L]
RewriteRule ^info/([[:digit:]]+)$ index.php?action=info&competence=$1 [NC,L,QSA]
The host I use is hostinger.fr but I don't know how to use the rewriteRules on it. (They said that it is activated and the first line (RewriteBase /) must be here)
Thanks for your help.
I'm working on a project which all pages need to have a querystring assumed id. for example following pages:
example.com/alfa.php?id=1
example.com/beta.php?id=30
exapmle.com/gamma.php?id=50
I'm using the following .htaccess to make my URLs beautiful.
RewriteEngine On
## BEAUTIFUL URL
DirectoryIndex default.php
Options -Multiviews -Indexes +FollowSymLinks
#RewriteBase /
DirectorySlash off
# remove trailing slash
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
# rewrite /dir/file?query to /dir/file.php?query
RewriteRule ^([\w\/-]+)(\?.*)?$ $1.php$2 [L,T=application/x-httpd-php]
This code will make the following convertion:
example.com/alfa.php?id=1 to example.com/alfa?id=1
Assuming that X.php is everytime changing (X) but id is always constant, What is the best content of .htaccess file?
I tried several methods, But what is the best?
and one thing more, How can I convert URL into example.com/alfa/1 AND example.com/beta/30
You're trying to match against the query string (all the stuff after ?) in the regex pattern of your rule. The query string isn't part of that match, only the URI. But since you don't want the query string to be touched, just don't match against it since it'll get appended automatically:
RewriteEngine On
## BEAUTIFUL URL
DirectoryIndex default.php
Options -Multiviews -Indexes +FollowSymLinks
#RewriteBase /
DirectorySlash off
# remove trailing slash
RewriteRule ^(.*)\/$ $1 [R=301,L]
# rewrite /dir/file?query to /dir/file.php?query
RewriteRule ^([\w/-]+)$ $1.php [L,T=application/x-httpd-php]
For example.com/alfa/1 and example.com/beta/30:
RewriteCond %{THE_REQUEST} [A-Z]{3,9}\ /([\w/-]+)(\.php)?\?id=([0-9]+)
RewriteRule ^ /%1/%3 [L,R=301]
RewriteRule ^([\w/-]+?)/([0-9]+)$ $1.php?id=$2 [L,QSA,T=application/x-httpd-php]
Something else you can try doing is getting rid of your rules and just using Multiviews, which you have turned off. Multiviews and mod_negotiation was made for stuff like this.
I have a site which uses apache mod_rewrite and has been working for the last 6 months with no error.
I have the following rewrite rule:
RewriteRule ^products/([a-z\-]+)/$ /products.php?category=$1 [NC,L]
Here is the code in my page products.php
$category = $_GET['category'];
if (isset($category)) {
// do some processing here
}
else {
header("Location: /500.html");
exit;
}
An example of a link which hits this rule is /products/lighting-poles/
Does anyone know why the actual rewrite is still occurring but not mapping the ([a-z\-]+) to category=$1?
Extra info
I noticed that the .htaccess file on the host has commented out the line Options +FollowSymLinks so I first tried to re-enable this only to have the site return an apache white screen 500 error.
More from the .htaccess file
<IfModule mod_rewrite.c>
#Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Add a trailing slash to paths without an extension
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]
# other rules including problem rule here
</IfModule>
This sounds morbidly like a multiviews issue. Multiviews is a content-negotiation thing where if it's turned on, it'll try a few different extra things in the URL-file mapping pipeline to try to find a file that maps to a URL. Here you have a URL like /products/blah, yet, you have a file called products.php, so multiviews might try to map products to /products.php/blah. Thus bypassing the rewrite rule entirely, and you don't see the category parameter.
Where the options are, try adding:
Options -Multiviews
i want rewrite it with a .htaccess i have this url:
../index.php?page=details&id=123456
like this:
../detail/123456
but I do not really know how to do this. Could you help me to rewrite this url?
or give me a simple example that I can understand how it works
RewriteRule ^(.*)/([0-9]+)$ index.php?page=$1&id=$2&%{QUERY_STRING}
The first (.*) selects any string, e.g. "details". ([0-9]+) catches a number, in your case "123456"
Finally
&%{QUERY_STRING}
ensures, that additional parameters are added to your backendscript, too.
This should work:
RewriteEngine On
RewriteRule ^detail/([^/]*)$ /index.php?page=details&id=$1 [L]
Here is an example I use for my webpage when it's in a subdirectory.
# "learn/degree/index.php?d=mathematics" shows as "learn/degree/mathematics"
RewriteRule ^learn/degree/([a-zA-Z_-]+)$ learn/degree/index.php?d=$1
To complete it, remember to write at the beginning of the page this:
RewriteBase /
RewriteEngine On
Here's the 'full' .htaccess I use in my page to give you some idea.
#Redirect any www. to the page without it. Helps to keep user logged.
RewriteBase /
RewriteEngine On
rewriteCond %{HTTP_HOST} ^www.newfutureuniversity.org [NC]
rewriteRule ^(.*)$ http://newfutureuniversity.org/$1 [R=301,L]
Options +Indexes
Options +FollowSymlinks
# Finally working. Rewrite the user so "/student/Username" will internally be "/student/?user=Username"
RewriteRule ^student/([a-zA-Z0-9_-]+)$ student/index.php?user=$1
# Rewrite the disciplines so "learn/degree/1" will internally be "learn/degree/index.php?dis=1"
RewriteRule ^learn/degree/([1-4])$ learn/degree/index.php?dis=$1
# Rewrite the degrees so "learn/degree/mathematics" will internally be "learn/degree/index.php?d=mathematics"
RewriteRule ^learn/degree/([a-zA-Z_-]+)$ learn/degree/index.php?d=$1
# Rewrite the degrees so "learn/subject/2/5/7" will internally be "learn/subject/index.php?class=2&div=5§ion=7"
RewriteRule ^learn/subject/([0-9])$ learn/subject/index.php?class=$1
RewriteRule ^learn/subject/([0-9])/([0-9])$ learn/subject/index.php?class=$1&div=$2
RewriteRule ^learn/subject/([0-9])/([0-9])/([0-9])$ learn/subject/index.php?class=$1&div=$2§ion=$3
You have to make the links to ../detail/123456, then the script interpretes it internally as ../index.php?page=details&id=123456. If you don't want this, then you are looking for a redirect.
Try this
<IfModule mod_rewrite.c>
RewriteEngine on
Options +FollowSymlinks
# RewriteBase / add this if necessery, commented intentionally
RewriteRule ^detail/([0-9]+)$ index.php?page=details&id=$2
</IfModule>