I want to remove 'articles' and 'artificial' folders from a site's urls, but when I try to introduce rewrite rules into .htaccess file I am receiving errors.
http://www.example.com/articles/artificial/are_string_beans_all_right_on_the_candida_diet.php
trying to convert
http://www.example.com/are_string_beans_all_right_on_the_candida_diet.php
here is the current version of my .htaccess file
DirectoryIndex index.php
AddDefaultCharset utf-8
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php/?$ / [L,R=301,NC]
RewriteRule ^((?!articles/artificial).*)$ articles/artificial/$1 [NC,L]
The problem should be on the last rule: what you need is a negative look behind ie anything not preceede with articles/artificial
The regex should be:
((?<!(articles/artificial)).*)
But this probabilly does not work since the .* is variable length (see regex look arrounds)
A solution could be replacing the negative look behind with a positive look ahead that allows for variable length. So your rule could be:
RewriteRule ^(?!.*?articles/artificial.*?)(.*)$ /articles/artificial/$1
Related
I need to replace underscores to dashes. I have tried this code and tried to change some parameters here, but i'm not yet friedly with .htaccess syntax so i failed to do it myself:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule !\.(html|php)$ - [S=6]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5-$6-$7 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5-$6 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_(.*)$ $1-$2-$3 [E=underscores:Yes]
RewriteRule ^([^_]*)_(.*)$ $1-$2 [E=underscores:Yes]
RewriteCond %{ENV:underscores} ^Yes$
RewriteRule (.*) /$1 [R=301,L]
My previous .htacces code was this, it was working fine, but there was no need to replace underscores to dashes till now:
# Added for SME compatibility
Options +FollowSymLinks
# Ignore Indexation of apache
IndexIgnore */*
# Turn on the RewriteEngine
RewriteEngine On
# Rules for ReRouting to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
I have URL like this: http://mydomain.tld/nl_BE/get_quote , the language (1st parameter) should NOT be replaced with dash, also language parameter can change, so i cannot hardcode language name in .htaccess file.
So i want to have URL something like this:
http://mydomain.tld/nl_BE/get-quote/some-random-title
http://mydomain.tld/nl_NL/about-us/some-random-text
Thanks in advance!
Rather than those multiple rules you can use this generic recursive rule:
Options +FollowSymLinks
RewriteEngine On
## RECURSION based rule
# if there is one underscore left then redirect
RewriteRule ^([a-z]{2}_[a-z]+)/([^_]*)_([^_]*)/?$ /$1/$2-$3 [NC,L,R=302]
# if there are more than one underscores then "repeatedly" replace it by -
RewriteRule ^([a-z]{2}_[a-z]+)/([^_]*)_(.*)$ $1/$2-$3 [L,NC]
I'm new to htaccess and i want to use RewriteRule in order to make urls more friendly but i have a problem.
Here is my htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule download/(.*)$ download.php?file=$1 [NC]
RewriteRule download/view/(.*)/(.*)$ download_view.php?name=$1&id=$2 [NC]
</IfModule>
Whatever i navigate to localhost/download/view/test/2/ for example, it appears as the first url localhost/download/test/
How to fix this ?
Turn around the order of those two rules, so that the more specialized one comes first. Then use the additional L flag to prevent further rewriting after a match:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule download/view/(.*)/(.*)$ download_view.php?name=$1&id=$2 [NC,L]
RewriteRule download/(.*)$ download.php?file=$1 [NC]
</IfModule>
Your first rewrite-rule already changes the URI:
RewriteRule download/(.*)$ download.php?file=$1 [NC]
Therefore localhost/download/view/test/2/ is already rewritten to download.php?file=view/test/2/ and the second rule you intentionally want to match against does not match any longer:
RewriteRule download/view/(.*)/(.*)$ download_view.php?name=$1&id=$2 [NC]
This does not match download.php?file=view/test/2/.
So you can solve this, by doing the check for download/view/... first, by flipping those two:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule download/view/(.*)/(.*)$ download_view.php?name=$1&id=$2 [NC]
RewriteRule download/(.*)$ download.php?file=$1 [NC]
</IfModule>
Always start with the most concrete/specific rule to rule out such problems. In more difficult cases, use RewriteCond to fine-tune when to apply a rewrite-rule.
I also suggest if you want to match at the beginning of the URI Paths, you should use the caret ("^") to mark the beginning in the regular expression:
RewriteRule ^download/view/(.*)/(.*)$ download_view.php?name=$1&id=$2 [NC]
RewriteRule ^download/(.*)$ download.php?file=$1 [NC]
Currently I working through my localhost in MAMP. I have read and research for the proper way to allow .htaccess file url rewrite and was successful. But now, the file is not formatting the links at all as desired. For example I have three pages index.php, about.php and contact. I am using MVC for the frame working of my site. Is this a problem with the code in the .htaccess file or my localserver?
Currently links look like this when going from one page to another:
localhost/mvc/index.php?
localhost/mvc/index.php?p=about
localhost/mvc/index.php?p=contact
.htaccess should format the links to look like this:
localhost/mvc/index/?
localhost/mvc/about/?
localhost/mvc/contact/?
.htaccess:
<IfModule mod_rewrite.c>
# Turn on the engine:
RewriteEngine on
# Set the base to this directory:
RewriteBase /mvc/
# Redirect certain paths to index.php:
RewriteRule ^(about|contact|this|that|search)/?$ index.php?p=$1
</IfModule>
Replace your existing .htaccess code fully with this code:
Options +FollowSymLinks -MultiViews
DirectoryIndex index.php index.html
# Turn mod_rewrite on
RewriteEngine On
# Set the base to this directory:
RewriteBase /mvc/
# Redirect /mvc/index.php?p=page to /mvc/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+mvc/index\.php\?p=([^\s]+) [NC]
RewriteRule ^ %1/? [R=302,L]
# Redirect /mvc/index.php to /mvc/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+mvc/index\.php\s [NC]
RewriteRule ^ /mvc/ [R=302,L]
# Internally forward certain /mvc/page/ to /mvc/index.php?p=page
RewriteRule ^(about|contact|this|that|search)/?$ /mvc/index.php?p=$1 [L,QSA,NC]
Replace
RewriteRule ^(about|contact|this|that|search)/?$ index.php?p=$1
with :
RewriteRule ^(about|contact|this|that|search)/\?$ index.php?p=$1
I just escaped the ? with a slash because, otherwise, you don't do this, the question mark will be interpreted and will means that the slash before it is optinal.
I need some htaccess rules which rewrite URIs like this:
http://sub.domain.tld/en/about?a=1&b=2
to this:
http://sub.domain.tld/about.php?lang=en&a=1&b=2
or more simple:
http://sub.domain.tld/about.php?a=1&b=2&lang=en
No difference...
But the user should see the first URI not the converted one (shouldn't be redirected).
You may try this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/]+) [NC]
RewriteCond %{QUERY_STRING} ([^/]+) [NC]
RewriteRule .* %2.php?%3&lang=%1? [L]
Maps silently
http://sub.domain.tld/LangCode/FileName?key1=val1&key2=val2
With or without trailing slash. Always displayed in browser's address bar,
To:
http://sub.domain.tld/FileName.php?key1=val1&key2=val2&lang=LangCode
For permanent redirect, replace [L] with [R=301,L]
Here is the .htaccess code :
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule en/about?a=(.*)&b=(.*)$ about.php?lang=en&a=$1&b=$2
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>