I have been trying to get my urls re-written. The first 4 rules are vital, but they are clashing with this line: (i think).
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.#?\ ]+)\.php([#?][^\ ]*)?\ HTTP/
this stops the url being able to be accessed like so www.example.com/page.php and redirects to www.example.com/page/
after adding the first 4 rules you can see in the htaccess, the above condition doesnt seem to work and Im able to access urls like this :(
lovelakedistrict.com/lake-district-cottages.php/cottages/5/
obviously i should be like this
lovelakedistrict.com/lake-district-cottages/cottages/5/
however this still works :)
lovelakedistrict.com/lake-district-cottages.php -->
lovelakedistrict.com/lake-district-cottages/
This is my htaccess file- can anyone see what happening?
Options +FollowSymlinks
Options +Indexes
RewriteEngine on
#these 4 rules stop being able to access pages like eg
#/lake-district-cottages/?cottages=5/
#/lake-district-cottages/?cottages/5/ --> all direct to /lake-district-cottages/cottages/5/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*(&+(.*))$
RewriteRule ^(.*[^/])/?$ /$1/%1/?%3 [N]
RewriteCond %{REQUEST_URI} !^/result
RewriteCond %{REQUEST_URI} !^/shop-result
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*$
RewriteRule ^(.*[^/])/?$ /$1/%1/?%4 [L,R=301]
RewriteCond %{REQUEST_URI} !^/result
RewriteCond %{REQUEST_URI} !^/shop-result
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*=/*([^&]*[^&/])/*(&+(.*))$
RewriteRule ^(.*[^/])/?$ /$1/%1/%2/?%4 [N]
RewriteCond %{REQUEST_URI} !^/result
RewriteCond %{REQUEST_URI} !^/shop-result
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*=/*([^&]*[^&/])/*$
RewriteRule ^(.*[^/])/?$ /$1/%1/%2/? [L,R=301]
RewriteCond %{REQUEST_URI} !^/result
RewriteCond %{REQUEST_URI} !^/shop-result
# this stops you accessing pages with php extention eg
#/lake-district-cottages.php --> directs to /lake-district-cottages/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.#?\ ]+)\.php([#?][^\ ]*)?\ HTTP/
# this ignors the include folder and does not add trailing slash (so ajax file works)
RewriteCond %1 !^include/
RewriteRule ^([^.]+)\.php$ /$1 [R=301,L]
#this removes php extention
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
#this removes php extention and adds trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
#these re-write urls allowing / to replace ? and =
RewriteRule ^lake-district-cottages/cottages/([0-9]+) lake-district-cottages.php?cottages=$1
RewriteRule ^lake-district-hotels/hotels/([0-9]+) lake-district-hotels.php?hotels=$1
RewriteRule ^lake-district-bed-and-breakfast/bed-and-breakfast/([0-9]+) lake-district-bed-and-breakfast.php?bed-and-breakfast=$1
RewriteRule ^lake-district-lodges/lodges/([0-9]+) lake-district-lodges.php?lodges=$1
RewriteRule ^lake-district-cottages-shop/lake-district-book-shop/([0-9]+) lake-district-cottages-shop.php?lake-district-book-shop=$1
RewriteRule ^lake-district-lodges/lodges/([0-9]+) lake-district-lodges.php?lodges=$1
RewriteRule ^result/([a-zA-Z0-9])/([0-9]+) result.php?$1=$2
RewriteRule ^shop-result/([a-zA-Z0-9])/([0-9]+) shop-result.php?$1=$2
The condition pattern doesn't match your URLs that contain PATH_INFO, but simplifying it a bit to something like this should take care of that:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.?\ ]+)\.php
RewriteCond %1 !^include/
RewriteRule ^([^.]+)\.php(/.+)?$ /$1%{PATH_INFO} [R=301,L]
Related
I have two different URLs:
1.: mysite.com/file.php
2.: mysite.com/**articles**.php?article=**something**
I would like to rewrite mysite.com/file.php to mysite.com/file.
AND
I would like to rewrite mysite.com/**articles**.php?article=something to mysite.com/**news**/**something**
I tried this but it's not working.
It rewrite mysite.com/file.php to mysite.com/file, but does not rewrite mysite.com/**articles**.php?article=**something** to mysite.com/**news**/**something**
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^?\ ]+)\.php
RewriteCond %{REQUEST_FILENAME} !exception.php
RewriteRule ^news/([^/]+)(/*)$ /articles.php?article=$1
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
(There is one exception in the code: exception.php which I don't want to rewrite.)
Can you please correct my code? I've been working on this for 4 days, I've read everything, but I can't do it.
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect for file.php file.
RewriteCond %{REQUEST_URI} !/?exception\.php [NC]
RewriteCond %{THE_REQUEST} \s/(file)\.php/?\s [NC]
RewriteRule ^ /%1? [R=301,L]
##Internal rewrite for php files.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ $1.php [QSA,L]
##external + internal rewrite rules for articile.php here.
RewriteCond %{THE_REQUEST} \s/articles\.php\?article=(\S+)\s [NC]
RewriteRule ^ /news/%1? [R=301,L]
RewriteRule ^news/([^/]*)$ articles.php?article=$1 [NC,QSA,L]
I have url with query in the midle of request uri ex:
http://domain.com/index.php?/recipe_category/recipe/
I've trying to remove the index.php as well but it leave the query sign (?) so the url become
http://domain.com/?/recipe_category/recipe/
How to remove all of the [index.php?]? For your information, my htaccess written as below :
RewriteEngine On
RewriteCond %{SERVER_NAME} !^www.domain.com$
RewriteRule .* http://www.domain.com%{REQUEST_URI} [R=301,L]
RewriteRule ^index.php/(.*)$ http://www.domain.com/$1 [R=302,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^index\.php$ / [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt|sitemap\.xml)
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
You can use following lines to remove "index.php?"
RewriteEngine On
RewriteRule ^index.php\?(.*)$ http://www.domain.com/$1 [R=302,L]
I'm not able to remove one GET parameter wrongly added by one 404 SEF module.
I want to remove "task=view" only if it appears alone and not with another parameter.
So
www.mysite.com/1.html?task=view should be redirected to www.mysite.com/1.html.
While www.mysite.com?task=view&view=article should remain unchanged.
No matter which RewriteRule I use, this paramater does not go. Looks like it is being generated from any environment variable when index.php is run.
For example this does not work:
RewriteCond %{QUERY_STRING} ^task=view$ [NC]
RewriteRule (.*)task=view.* $1 [R=301,L]
Here is my .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
RewriteCond %{QUERY_STRING} base64_encode.*(.*) [OR]
RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|[|\%[0-9A-Z]{0,2})
RewriteRule ^(.*)$ index.php [F,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|.php|.html|.htm|.feed|.pdf|.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
Options -Indexes
What is the way to handle this?
Use this rule:
RewriteCond %{QUERY_STRING} ^task=view$ [NC]
RewriteRule ^(.*)$ $1? [R=301,L]
? at the end of $1 will strip-off any existing query string.
I've been trying to delete the .php extension and force trailing slashes in the end of the URLs.
I've found an answer to do it exactly what I need at (.htaccess trouble with hiding file extension and forcing trailing slash) but it does not work, I believe it's due to how I am forcing my website to change the document root.
As I do not have access to Virtual Hosts I must change my document root using htaccess, and I have accomplished it by applying the following code..
RewriteCond %{HTTP_HOST} ^site.co$ [NC]
RewriteRule ^(.*)$ http://www.site.co/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !webroot/
RewriteRule (.*) /webroot/$1 [L]
The only working solution that I've been able to apply to hide the .php extension is with the code below.
removing .php extension(only working solution)
RewriteCond %{THE_REQUEST} ^\w+\ /(.*)\.php(\?.*)?\ HTTP/
RewriteRule ^ http://%{HTTP_HOST}/%1 [R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .* $0.php
This code is in the thread that I have linked above, but it does not work on my website as I believe it's due to the document root.
RewriteEngine on
# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?[^/])$ /$1/ [R=301,L]
# .php ext hiding
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
Try these modified rules with different ordering:
RewriteEngine on
# add www before hostname
RewriteCond %{HTTP_HOST} ^site\.co$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]
# if on article page, get slugs and make into friendly url
RewriteCond %{THE_REQUEST} \s/article\.php\?article_uid=([^&]+)&article_title=([^&\ ]+)
RewriteRule ^ /article/%1/%2/? [L,R=302,NE]
# if page with .php is requested then remove the extension
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L,NE]
# Force a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s/+([^.]+?[^/.])[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L]
# allow page direction to change the slugs into friendly seo URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?:^|/)article/([^/]+)/([^/]+)/?$ /webroot/article.php?article_uid=$1&article_title=$2 [L,QSA,NC]
# silently rewrite to webroot
RewriteCond %{REQUEST_URI} !/webroot/ [NC]
RewriteRule ^ /webroot%{REQUEST_URI} [L]
# .php ext hiding
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
I am trying to serve different urls using mod_rewrite but whatever I try it is just not working.
An example url would be
http://www.site.com/country/tours/dynamic-part/?&city=new-york,los-angeles
And I am trying to change the url using .htaccess to:
http://www.site.com/country/tours/dynamic-part/new-york,los-angeles
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)city=([^&]*)(&|$)
RewriteRule ^country\/tours\/([a-zA-Z0-9]*)\/.+city=([^\/]*)$ http://www.site.com/country/tours/$1/$2 [L,R=301]
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Any ideas? I though I was close but not anymore :/
The RewriteRule does NOT match the query string, see
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#what_is_matched
So the .+city part of the rule will never match.
This should work tho...
RewriteCond %{QUERY_STRING} (^|&)city=([^&]*)(&|$)
RewriteRule ^country\/tours\/([a-zA-Z0-9]*)\/ http://www.site.com/country/tours/$1/%2 [L,R=301]
The subsitution can read back-referenecs to the RewriteCond pattern.