I have this snippet in my .htaccess file:
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^(20\d\d)/page/([0-9]+)/$ ?yearMeasure=$1&page=$2 [L]
RewriteRule ^(20\d\d)/$ ?yearMeasure=$1 [L]
RewriteRule ^category/([A-Za-z0-9-]+)/page/([0-9]+)/$ ?category=$1&page=$2 [L]
RewriteRule ^category/([A-Za-z0-9-]+)/$ ?category=$1 [L]
RewriteRule ^(20\d\d)/([A-Za-z0-9-]+)/$ ?type=post&year=$1&title=$2 [L,QSA]
However, when I remove the first line, the website still behaves in the same way. The URLs still remain "clean." This brings me to my question; what is the significance of RewriteCond in this snippet?
The only RewriteRule this RewriteCond affected was this:
RewriteRule ^page/([0-9]+)/$ ?page=$1 [L]
...when I attempted to visit /page/2/, it didn't work.
When I moved it above the RewriteCond as follows:
RewriteRule ^page/([0-9]+)/$ ?page=$1 [L]
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^(20\d\d)/page/([0-9]+)/$ ?yearMeasure=$1&page=$2 [L]
RewriteRule ^(20\d\d)/$ ?yearMeasure=$1 [L]
RewriteRule ^category/([A-Za-z0-9-]+)/page/([0-9]+)/$ ?category=$1&page=$2 [L]
RewriteRule ^category/([A-Za-z0-9-]+)/$ ?category=$1 [L]
RewriteRule ^(20\d\d)/([A-Za-z0-9-]+)/$ ?type=post&year=$1&title=$2 [L,QSA]
...everything came back to normal. Why?
In the docs RewriteRule is defined this way:
RewriteRule Pattern Substitution [flags]
If Pattern matches the url requested (domain.com/path/file.cgi), the url is replaced with the Substitution (either a redirect header is returned or the url is changed internally depending on the flags)
This rewrite rule:
RewriteRule ^(20\d\d)/page/([0-9]+)/$ ?yearMeasure=$1&page=$2 [L]
will check if the url matches the pattern and the replacement will occur only in this case. That's why here such RewriteCond is useless:
RewriteCond %{QUERY_STRING} id=([0-9]+)
It's useful in situations that RewriteRule can't capture like checking the hostname or whether a file exists:
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule .? http://example.com%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
http://www.sitepoint.com/apache-mod_rewrite-examples-2/
Related
For a website I have locally, I need to set the display language as if it were a subfolder, using an htaccess file.
For example, if I choose to display the site in German, instead of displaying it like this => local_ip/test/index.php?lang=de, I would like it to be displayed like this => local_ip/test/de/ .
Also, if I type in the url without setting the language, I would like htaccess to automatically redirect to the German language, from so => local_ip/test/tour/mountain/ to so => local_ip/test/de/tour/mountain .
My current htaccess file is structured as follows:
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{QUERY_STRING} ^t=(.+?)
RewriteRule ^city/(.+?)/?$ city.php?url=$1&t=%1 [NC,L]
RewriteCond %{QUERY_STRING} ^p=([0-9]+)
RewriteRule ^city/(.+?)/?$ city.php?url=$1&p=%1 [NC,L]
RewriteCond %{QUERY_STRING} ^t=(.+?)&p=([0-9]+)
RewriteRule ^city/(.+?)/?$ city.php?url=$1&t=%1&p=%2 [NC,L]
RewriteCond %{QUERY_STRING} ^c=true
RewriteRule ^city/(.+?)/?$ city.php?url=$1&c=true [NC,L]
RewriteRule ^city/(.+?)$ city.php?url=$1 [NC,L]
RewriteRule ^tour/(.+?)$ tour.php?url=$1 [NC,L]
SOLVED
After several attempts and after reading several support requests posted in this community, I found a way to make the system recognise all the other rules as well. All I had to do was put them before the last rule provided by the user who supported me. Here is the final code.
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} !/de/ [NC]
RewriteRule (.*) /test/de/$1 [R,L]
RewriteRule ^de/$ /test/index.php?lang=de [L]
RewriteCond %{QUERY_STRING} ^t=(.+?)
RewriteRule city/(.+?)/?$ city.php?url=$1&t=%1&lang=de [NC,L]
RewriteCond %{QUERY_STRING} ^p=([0-9]+)
RewriteRule city/(.+?)/?$ city.php?url=$1&p=%1&lang=de [NC,L]
RewriteCond %{QUERY_STRING} ^t=(.+?)&p=([0-9]+)
RewriteRule city/(.+?)/?$ city.php?url=$1&t=%1&p=%2&lang=de [NC,L]
RewriteCond %{QUERY_STRING} ^c=true
RewriteRule city/(.+?)/?$ city.php?url=$1&c=true&lang=de [NC,L]
RewriteRule city/(.+?)$ city.php?url=$1&lang=de [NC,L]
RewriteRule tour/(.+?)$ tour.php?url=$1&lang=de [NC,L]
RewriteRule ^de/(.*)$ /test/$1 [L]
You can use the following :
RewriteEngine on
#redirect URLs to include /de
#RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/de/ [NC]
RewriteRule (.*) /test/de/$1 [R,L]
# /test/index.php?lang=de to /test/de
RewriteRule ^de/$ /test/index.php?lang=de [END]
RewriteRule ^de/(.+)$ /test/$1 [END]
My website runs a script called -> WSS wallpaper script
My Problem -> I have been trying to force remove or add trailing slash to the end of my URL to prevent duplicated content and also to clean up my URLs.
I have tried all sorts and tried everything I could think of and loads from the interwebs but no such luck yet! It might be a quick fix but I have looked at it so much I am probably blind to something dead obvious.
So I present you with all my .htaccess code:
DirectoryIndex index.php
RewriteEngine on
RewriteRule ^download/([0-9]+)?/([0-9]+)x([0-9]+)/([^/\.]+) image.php?id=$1&width=$2&height=$3&cropratio=$4&download=1 [L]
RewriteRule ^file/([0-9]+)?/([0-9]+)x([0-9]+)/([^/\.]+) image.php?id=$1&width=$2&height=$3&cropratio=$4 [L]
RewriteRule ^preview/([0-9]+)?/([0-9]+)x([0-9]+)/([^/\.]+) wallpaper_preview.php?id=$1&width=$2&height=$3&name=$4 [L]
RewriteRule ^thumbnail/([0-9]+)?/([0-9]+)x([0-9]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/]+) image.php?wallpaper_id=$1&width=$2&height=$3&cropratio=$4&align=$5&valign=$6&file=$7 [L]
RewriteRule ^cat/([0-9]+)?/([^/\.]+)/p([0-9]+) index.php?task=category&id=$1&name=$2&page=$3 [L]
RewriteRule ^cat/([0-9]+)?/([^/\.]+)/([0-9a-zA-Z?-]+)/p([0-9]+) index.php?task=category&id=$1&name=$2&sortby=$3&page=$4 [L]
RewriteRule ^cat/([0-9]+)?/([^/\.]+)/([0-9a-zA-Z?-]+)-([0-9]+) index.php?task=category&id=$1&sortby=$3&page=$4 [L]
RewriteRule ^cat/([0-9]+)?/([^/\.]+) index.php?task=category&id=$1&name=$2 [L]
RewriteRule ^tag/([^/\.]+)/([0-9a-zA-Z?-]+)/([0-9]+) index.php?task=tag&t=$1&sortby=$2&page=$3 [L]
RewriteRule ^tag/([^/\.]+) index.php?task=tag&t=$1 [L]
RewriteRule ^profile/([0-9]+)?/([^/\.]+) index.php?task=profile&id=$1&name=$2 [L]
RewriteRule ^profile/comments/([0-9]+)?/([^/\.]+) index.php?task=users_comments&id=$1&name=$2 [L]
RewriteRule ^page/([0-9]+) index.php?task=view_page&id=$1 [L]
RewriteRule ^register index.php?task=register [L]
RewriteRule ^lost-password index.php?task=lost_pass [L]
RewriteRule ^links index.php?task=links [L]
RewriteRule ^news/item/([0-9]+)/([^/\.]+) index.php?task=news&id=$1 [L]
RewriteRule ^news/page([0-9]+) index.php?task=news&page=$1 [L]
RewriteRule ^members/([^/\.]+)-([^/\.]+)/page([0-9]+)? index.php?task=member_list&sort=$1&order=$2&page=$3 [L]
RewriteRule ^members index.php?task=member_list [L]
RewriteRule ^messages index.php?task=messages [L]
RewriteRule ^submit index.php?task=submit [L]
RewriteRule ^search/([^/\.]+) index.php?task=search&q=$1 [L]
RewriteRule ^search index.php?task=search [L]
RewriteRule ^submit index.php?task=submit [L]
RewriteRule ^r-([0-9]+)?-([0-9]+)? go.php?id=$1&ref=$2 [L]
RewriteRule ^r-([0-9]+)? go.php?id=$1 [L]
RewriteRule ^([^/\.]+)/([0-9]+)/([^/\.]+) index.php?task=view&id=$2&name=$3 [L]
RewriteRule ^news/([^/\.]+) index.php?task=news&name=$1 [L]
RewriteRule ^profile/([^/\.]+) index.php?task=profile&name=$1 [L]
RewriteRule ^news index.php?task=news [L]
RewriteRule ^page/([^/\.]+) index.php?task=view_page&name=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/([0-9a-zA-Z'?-]+)/([0-9]+) index.php?task=category&name=$1&sortby=$2&page=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/([^/\.]+) index.php?task=view&name=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+) index.php?task=category&name=$1 [L]
## www reslove ##
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
## www reslove ##
## index reslove ##
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ http://www.epicwallpaper.net/$1 [R=301,L]
## index reslove ##
Right below the RewriteEngine On line, add:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R] # <- for test, for prod use [L,R=301]
to enforce a no-trailing-slash policy.
To enforce a trailing-slash policy:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R] # <- for test, for prod use [L,R=301]
EDIT: commented the R=301 parts because, as explained in a comment:
Be careful with that R=301! Having it there makes many browsers cache the .htaccess-file indefinitely: It somehow becomes irreversible if you can't clear the browser-cache on all machines that opened it. When testing, better go with simple R or R=302
After you've completed your tests, you can use R=301.
To complement Jon Lin's answer, here is a no-trailing-slash technique that also works if the website is located in a directory (like example.org/blog/):
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
For the sake of completeness, here is an alternative emphasizing that REQUEST_URI starts with a slash (at least in .htaccess files):
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*)/$
RewriteRule ^ /%1 [R=301,L] <-- added slash here too, don't forget it
Just don't use %{REQUEST_URI} (.*)/$. Because in the root directory REQUEST_URI equals /, the leading slash, and it would be misinterpreted as a trailing slash.
If you are interested in more reading:
PR 3145 for Laravel
A discussion on commit 343c31e
(update: this technique is now implemented in Laravel 5.5)
This is what I've used for my latest app.
# redirect the main page to landing
##RedirectMatch 302 ^/$ /landing
# remove php ext from url
# https://stackoverflow.com/questions/4026021/remove-php-extension-with-htaccess
RewriteEngine on
# File exists but has a trailing slash
# https://stackoverflow.com/questions/21417263/htaccess-add-remove-trailing-slash-from-url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)/+$ /$1 [R=302,L,QSA]
# ok. It will still find the file but relative assets won't load
# e.g. page: /landing/ -> assets/js/main.js/main
# that's we have the rules above.
RewriteCond %{REQUEST_FILENAME} !\.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*?)/?$ $1.php
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
## hide .html extension
# To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+).html
RewriteRule ^ %1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)/\s
RewriteRule ^ %1 [R=301,L]
## To internally redirect /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^\.]+)$ $1.html [L]
<Files ~"^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
satisfy all
</Files>
This removes html code or php if you supplement it. Allows you to add trailing slash and it come up as well as the url without the trailing slash all bypassing the 404 code. Plus a little added security.
I have old site with links like this:
http://domain.com/image.php?690
And I would like to change it into:
http://domain.com/old690
I have tried many different Rules, fe.:
RewriteRule ^image.php?(.+) old$1 [L]
EDIT: All rules looks like:
RewriteEngine On
RewriteRule ^admin/(.*) ninja-admin/$1 [L]
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
RewriteRule ^image.php\?(.+) old$1 [L]
What is correct RewriteRule, and why?
You're doing it upside down.
Put this code in your htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^old([0-9]+)$ image.php?$1 [L]
RewriteRule ^admin/(.*)$ ninja-admin/$1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
The rule for your images is the following
RewriteRule ^old([0-9]+)$ image.php?$1 [L]
It will forward every url like /old123 (where 123 is one or more digits) to image.php?123
EDIT: if you want to forbid direct access to image.php?xxx then you can do it this way
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/image\.php\?([0-9]+) [NC]
RewriteRule ^ old%1 [R=301,L]
RewriteRule ^old([0-9]+)$ image.php?$1 [L]
RewriteRule ^admin/(.*)$ ninja-admin/$1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
I use this so http://example.com/old4444 forwards to http://example.com/image.php?file=4444.
Also, the browser will keep http://example.com/old4444 in the address bar.
RewriteCond %{QUERY_STRING} !marker
RewriteCond %{QUERY_STRING} file=([0-9]+)
RewriteRule ^/?image.php$ %1? [R=301,L]
RewriteRule ^/?old([-a-zA-Z0-9_+]+)$ image.php?marker&file=$1 [L]
I'm having a problem with my .htaccess mod_rewrite. I've made a simple custom CMS and i've put this in a sub directory of my domain; http://www.example.com/cms
I've got the following situations:
cms/index.php?page=modules/pages/index.php
convert to
cms/modules/pages/index
and
cms/index.php?page=modules/pages/edit.php?id=1
convert to
cms/modules/pages/edit/1
I've got it working with a subdomain, but when i use example.com/cms it doesn't do anything
i've made this .htaccess but i couldn't get it working...
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule ^(.*?)$ $1 [L]
RewriteRule ^/cms/([^/]*)/$ cms/index.php?page=$1 [L,QSA]
RewriteRule ^/cms/([^/]*)$ /cms/index.php?page=$1 [L,QSA]
RewriteRule ^/cms/pages/([^/]*)/$ /cms/index.php?page=pages/$1 [L,QSA]
RewriteRule ^/cms/pages/([^/]*)$ /cms/index.php?page=pages/$1 [L,QSA]
RewriteRule ^/cms/modules/([^/]*)/([^/]*)/$ /cms/index.php?page=modules/$1/$2 [L,QSA]
RewriteRule ^/cms/modules/([^/]*)/([^/]*)$ /cms/index.php?page=modules/$1/$2 [L,QSA]
RewriteRule ^/cms/modules/([^/]*)/([^/]*)/([^/]*)$ /cms/index.php?page=modules/$1/$2&id=$3 [L,QSA]
If this is in an htaccess file, the URI's have their prefix removed (the leading slash), so those patterns won't match anything. Either remove the leading slash from the pattern or make it optional (with a ?):
RewriteRule ^/?cms/([^/]*)/$ cms/index.php?page=$1 [L,QSA]
RewriteRule ^/?cms/([^/]*)$ /cms/index.php?page=$1 [L,QSA]
RewriteRule ^/?cms/pages/([^/]*)/$ /cms/index.php?page=pages/$1 [L,QSA]
RewriteRule ^/?cms/pages/([^/]*)$ /cms/index.php?page=pages/$1 [L,QSA]
RewriteRule ^/?cms/modules/([^/]*)/([^/]*)/$ /cms/index.php?page=modules/$1/$2 [L,QSA]
RewriteRule ^/?cms/modules/([^/]*)/([^/]*)$ /cms/index.php?page=modules/$1/$2 [L,QSA]
RewriteRule ^/?cms/modules/([^/]*)/([^/]*)/([^/]*)$ /cms/index.php?page=modules/$1/$2&id=$3 [L,QSA]
Additionally, your first rule can be simplified to just:
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule ^ - [L]
I have a website http://rochesterwaterskishow.com which they've recently changed their name so they want to update their url to http://skidox.com. I'm trying to redirect any page from rochesterwaterskishow.com to skidox.com/site/index.
I have this line of code which redirects http://rochesterwaterskishow.com to http://skidox.com, but if I go to something like http://rochesterwaterskishow.com/test, it doesn't redirect to http://skidox.com.
RewriteRule ^$ http://skidox.com/site/index [R=301,L]
How can I make it a catch all so anything rochesterwaterskishow.com/* gets redirected to skidox.com/site/index?
UPDATE: Full .htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteRule ^$ http://skidox.com/site/index [R=301,L]
That's because the search pattern ^$ will only match a URI path of "/". You need to pick up the request in a match variable, for example:
RewriteCond %{HTTP_HOST} rochesterwaterskishow
RewriteRule ^.* http://skidox.com/site/index/$0 [R=301,L]
I am assuming that you are using SEO optimised-style URIs for the new site. If you want to simply redirect everything to the index page without any context, then you still need a pattern that matches:
RewriteCond %{HTTP_HOST} rochesterwaterskishow
RewriteRule ^ http://skidox.com/site/index [R=301,L]
Update following post of full htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} rochesterwaterskishow
RewriteRule ^.* http://skidox.com/$0 [R=301,L]
RewriteCond $0 ^(index\.php$|robots\.txt$|resources)
RewriteRule ^.* - [S=1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteRule ^$ http://skidox.com/site/index/$1 [R=301,L]