How can I rewrite:
http://localmachine/product.php?c=water&p=Evian
into:
http://localmachine/water/evian
If I add
RewriteRule ^([A-Za-z0-9_?-]+)/([A-Za-z0-9_?-]+)(/)?$ product.php?c=$1&p=$2 [NC,L]
I'm getting a conflict I suppose at this line
RewriteRule ^(index\.php)/([^-]+)-([^/]+)(/.*)?$ /$1$4?$2=$3 [L,QSA]
This is my htaccess :
RewriteEngine On
Options -Indexes
Options +FollowSymLinks
<Files .htaccess>
deny from all
</Files>
# rewrite /user-options to /user.php?page=page
RewriteRule ^user/logout/? actions/handler.user.php?logout=true [NC,L]
RewriteRule ^user/?$ user.php [NC,L]
# edit shopping lists
RewriteRule ^user/liste/([0-9]+)/del(/)?$ user.php?page=edit-list&gid=$1&action=del [NC,L]
RewriteRule ^user/liste/(.+)$ user.php?page=edit-list&gid=$1 [NC,L]
RewriteRule ^user/([^/]+)/?$ user.php?page=$1 [L,QSA]
# rewrite /category to /index.php?c=category
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php/?c=$1 [L,QSA]
# rewrite /category/brand-mybrand/country-mycountry/offer-yes/new-yes
# to /index.php/brand-mybrand/country-mycountry/offer-yes/new-yes?c=category
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^-]+.+)/?$ /index.php/$2?c=$1 [L,QSA]
# rewrite /index.php/brand-mybrand/country-mycountry/offer-yes/new-yes?c=category
# converts any /name-val/ to query parameter name=val in every rewrite
# stopping when there is no part left after /index.php
RewriteRule ^(index\.php)/([^-]+)-([^/]+)(/.*)?$ /$1$4?$2=$3 [L,QSA]
# review basket & place order
RewriteRule ^shopping-cart/actions/delete/(.*)?$ actions/handler.comanda.php?action=delcart&id=$1 [NC,L]
RewriteRule ^shopping-cart/del/(.*)/? cosuldecumparaturi.php?d=true&p=$1 [NC,L]
RewriteRule ^shopping-cart/place-order/?$ comanda.php [NC,L]
RewriteRule ^shopping-cart/preview(/)?$ cosuldecumparaturi.php [NC,L]
# rewrite ajax calls
RewriteRule ^ajax/totalShoppingCart/? ajax/totalShoppingCart.php [NC,L]
# rewrite information page
RewriteRule ^informatii/([A-Za-z0-9_?-]+)/?$ pages.php?page=$1 [NC,L]
Related
I have a URL Structure as mysite.com/category.php?c=abc&page=4
I need to have a URL Structure as mysite.com/category/abc/page/4
My Htaccess File code for this rewrite looks like this.
RewriteEngine On
<IfModule mod_rewrite.c>
https redirect Rule Here
</IfModule>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^([-\w]+)$ blog-post.php?slug=$1 [NC,L]
RewriteRule ^amp/([-\w]+)$ amp/amp.php?slug=$1 [NC,L]
RewriteRule ^mirror/([-\w]+)$ mirror/post.php?slug=$1 [NC,L]
RewriteRule ^category/([a-zA-Z-]+) category.php?c=$1 [NC,L]
RewriteRule ^category/([a-zA-Z-/]+)/page/([0-9]+) category.php?c=$1&page=$2 [NC,L]
RewriteRule ^mirror/category/([a-zA-Z-]+) mirror/category.php?c=$1 [NC,L]
RewriteRule ^feed/([a-zA-Z-]+)$ feed/feed.php?f=$1 [NC,L]
RewriteRule ^author/([a-zA-Z0-9-]+) author.php?name=$1 [NC,L]
RewriteRule ^tag/([a-zA-Z0-9-]+) tag.php?t=$1 [NC,L]
RewriteRule ^mirror/tag/([a-zA-Z0-9-]+) mirror/tag.php?t=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
RewriteRule ^sitemap\.xml/?$ sitemap.php
RewriteRule ^news-sitemap\.xml/?$ news-sitemap.php
RewriteRule ^image-sitemap\.xml/?$ image-sitemap.php
RewriteRule ^author-sitemap\.xml/?$ author-sitemap.php
ErrorDocument 404 /404.php
ErrorDocument 500 Error
Options -Indexes
The problem I am facing is when I try to get the page number using the $_get[] variable, I am not able to fetch it. Rest I am able to get the ABC through $_get[]
You have big set of rules. Let me make an attempt to get you going:
ErrorDocument 404 /404.php
Options -Indexes -MultiViews
RewriteEngine On
<IfModule mod_rewrite.c>
https redirect Rule Here
</IfModule>
## Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/+$
RewriteRule ^ %1 [R=301,NE,L]
## ignore all URIs for actual files and directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^amp/([-\w]+)$ amp/amp.php?slug=$1 [NC,L,QSA]
RewriteRule ^mirror/([-\w]+)$ mirror/post.php?slug=$1 [NC,L,QSA]
RewriteRule ^category/([\w-]+)$ category.php?c=$1 [NC,L,QSA]
RewriteRule ^category/([\w-]+)/page/(\d+)$ category.php?c=$1&page=$2 [NC,L,QSA]
RewriteRule ^mirror/category/([\w-]+)$ mirror/category.php?c=$1 [NC,L,QSA]
RewriteRule ^feed/([\w-]+)$ feed/feed.php?f=$1 [NC,L,QSA]
RewriteRule ^author/([\w-]+)$ author.php?name=$1 [NC,L,QSA]
RewriteRule ^tag/([\w-]+)$ tag.php?t=$1 [NC,L,QSA]
RewriteRule ^mirror/tag/([\w-]+)$ mirror/tag.php?t=$1 [NC,L,QSA]
RewriteRule ^sitemap\.xml$ sitemap.php [NC,L]
RewriteRule ^((?:news|image|author)-sitemap)\.xml$ $1.php [NC,L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteRule ^([-\w]+)$ blog-post.php?slug=$1 [QSA,L]
Make sure to test it after clearing your browser cache completely or test it from command line curl.
With your shown samples, please try following. Please make sure to clear your browser cache before testing your URLs.
There are 2 important points you need to take care. 1st- You didn't use $ anchor which makes sure your 1st rule matching only URI like http://locahost:80/category/testtest so what's happening your rules are matching both the URIs. 2nd- Is you need to add conditions to make sure these rules are applied to only non existing stuff(in ideal scenario).
RewriteEngine On
Options -Indexes
<IfModule mod_rewrite.c>
##https redirect Rule Here
</IfModule>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^([-\w]+)$ blog-post.php?slug=$1 [NC,L]
RewriteRule ^amp/([-\w]+)$ amp/amp.php?slug=$1 [NC,L]
RewriteRule ^mirror/([-\w]+)$ mirror/post.php?slug=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([a-zA-Z-]+)/?$ category.php?c=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([a-zA-Z-/]+)/page/([0-9]+)/?$ category.php?c=$1&page=$2 [NC,L]
RewriteRule ^mirror/category/([a-zA-Z-]+) mirror/category.php?c=$1 [NC,L]
RewriteRule ^feed/([a-zA-Z-]+)$ feed/feed.php?f=$1 [NC,L]
RewriteRule ^author/([a-zA-Z0-9-]+)$ author.php?name=$1 [NC,L]
RewriteRule ^tag/([a-zA-Z0-9-]+)$ tag.php?t=$1 [NC,L]
RewriteRule ^mirror/tag/([a-zA-Z0-9-]+)$ mirror/tag.php?t=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
RewriteRule ^sitemap\.xml/?$ sitemap.php [NC,L]
RewriteRule ^news-sitemap\.xml/?$ news-sitemap.php [NC,L]
RewriteRule ^image-sitemap\.xml/?$ image-sitemap.php [NC,L]
RewriteRule ^author-sitemap\.xml/?$ author-sitemap.php [NC,L]
ErrorDocument 404 /404.php
ErrorDocument 500 Error
This question has been asked too many times, but I don't realize what I'm doing wrong.
I want to mention that I am new to htaccess and mod_rewrite.
I have the page http://localhost/product.php?c=category&p=product which I'd like to rewrite to http://localhost/category/product.
Tried to change the ruler order but the other rulers no longer works.
Htaccess code:
# rewrite search page
RewriteRule ^q-([^/]+)/page-(.*)/?$ search.php?q=$1&page=$2 [NC,L]
RewriteRule ^q-([^/]+)/?$ search.php?q=$1 [NC,L]
RewriteRule ^q/?$ search.php?q [NC,L]
RewriteRule cautare(/)? cautare.php [NC,L]
RewriteRule ^multisearch/?$ multisearch.php [NC,L]
# rewrite /category to /index.php?c=category
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#----------------Old version----------------------#
RewriteRule ^([^/]+)/?$ /index.php/?c=$1 [L,QSA]
# rewrite /category to /index.php?t=tab
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^t/([^/]+)/?$ /index.php/?t=$1 [L,QSA]
RewriteRule ^t/([^/]+)/([^-]+.+)/?$ /index.php/$2?t=$1 [L,QSA]
# rewrite /category/brand-mybrand/country-mycountry/offer-yes/new-yes
# to /index.php/brand-mybrand/country-mycountry/offer-yes/new-yes?c=category
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#----------------Old version----------------------#
RewriteRule ^([^/]+)/([^-]+.+)/?$ /index.php/$2?c=$1 [L,QSA]
# rewrite /index.php/brand-mybrand/country-mycountry/offer-yes/new-yes?c=category
# converts any /name-val/ to query parameter name=val in every rewrite
# stopping when there is no part left after /index.php
RewriteRule ^(index\.php)/([^-]+)-([^/]+)(/.*)?$ /$1$4?$2=$3 [L,QSA]
# rewrite product page
RewriteRule ^([A-Za-z0-9_?-]+)/([A-Za-z0-9_?-]+)(/)?$ product.php?c=$1&p=$2 [L,QSA]
As it is, if I click on the product page nothing happens(just a refresh).
Sorry for my english.
Your .htaccess appears to be overly complicated. I am making an attempt to simplify this a bit:
# rewrite search page
RewriteRule ^q-([^/]+)/page-(.*)/?$ search.php?q=$1&page=$2 [NC,L,QSA]
RewriteRule ^q-([^/]+)/?$ search.php?q=$1 [NC,L,QSA]
RewriteRule ^q/?$ search.php?q [NC,L,QSA]
RewriteRule ^(cautare|multisearch)/?$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# rewrite /category to /index.php?t=tab
RewriteRule ^t/([^/]+)/?$ index.php/?t=$1 [L,QSA,NC]
RewriteRule ^t/([^/]+)/([^-]+.+)/?$ index.php/$2?t=$1 [L,QSA,NC]
# rewrite /index.php/brand-mybrand/country-mycountry/offer-yes/new-yes?c=category
# converts any /name-val/ to query parameter name=val in every rewrite
# stopping when there is no part left after /index.php
RewriteRule ^(index\.php)/([^-]+)-([^/]+)(/.*)?$ /$1$4?$2=$3 [L,QSA,NC]
# rewrite /category/brand-mybrand/country-mycountry/offer-yes/new-yes
# to /index.php/brand-mybrand/country-mycountry/offer-yes/new-yes?c=category
RewriteRule ^([^/]+)/([^-]+-.+)/?$ index.php/$2?c=$1 [L,QSA]
# rewrite product page
RewriteRule ^([\w-]+)/([\w-]+)/?$ product.php?c=$1&p=$2 [L,QSA]
# rewrite /category to /index.php?c=category
RewriteRule ^([^/]+)/?$ index.php/?c=$1 [L,QSA]
Duplicate
The redirect using .htaccess doesn't redirect links with .php to without .php
.htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
</IfModule>
# END WordPress
Even then I could see that the example.com/example.php redirects to 404.
Any ideas or suggestions?
You must change the order of directives. Use:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
My URL:
localhost/categories?DPt=MQ==&CTg=MQ==&NLs=Mw==
I want so:
localhost/product-a/departure-a/categories-a/
My htaccess:
<IfModule mod_rewrite.c>
RewriteRule ^confirmation/?$ confirmation.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?([^/]+)/?$ products.php?PDt2=$1 [QSA,NC,L]
RewriteRule ^/?categories/([^/]+)/?$ categories.php?DPt=$1&CTg=$2&NLs=$3 [QSA,NC,L]
</IfModule>
But it does not.
What is wrong?
This is my version of .htaccess with comments inside:
<IfModule mod_rewrite.c>
# stop rewrite if matches existing file or directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* - [L]
# redirect trailing slash
RewriteRule ^(.+)/$ /$1 [R=301,L,QSA,NE]
# confirmation
RewriteRule ^confirmation$ /confirmation.php [L]
# match everything except slash to product
RewriteRule ^([^/]+)$ /products.php?PDt2=$1 [QSA,L]
# match categories
RewriteRule ^(.+)/(.+)/(.+)$ /categories.php?DPt=$1&CTg=$2&NLs=$3 [QSA,L]
</IfModule>
I'm having trouble re-writing a simple URL with .htaccess.
The URL I am trying to rewrite is:
http://www.domain.com/index.php?page=PAGE_NAME
I would like the PAGE_NAME to be directly after the domain, for example if PAGE_NAME is blog:
http://www.domain.com/blog
At the moment I have tried the following with no success:
RewriteRule ^/?([a-zA-Z0-9_-]+)?$ index.php?page=$1
All help is really appreciated.
Thanks in advance.
My current .htaccess file:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
ErrorDocument 404 /search.php?page=notfound
# Add WWW to URL
RewriteCond %{HTTP_HOST} ^cristianrgreco\.com$ [NC]
RewriteRule ^(.*)$ http://www.cristianrgreco.com/$1 [L,R=301]
# Remove trailing slashes from end of URL
RewriteCond %{HTTP_HOST} !^\.cristianrgreco\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [L,R=301]
# Rewrite main page URLs
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=([^\ ]+) [NC]
RewriteRule ^ %1? [L,R=301]
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1 [L,NC]
# Rewrite download URLs
RewriteRule ^download/([a-zA-Z0-9._-]+)/?$ download.php?file=$1
# Rewrite page navigation links
RewriteRule ^(.+?)/page-([a-zA-Z0-9_-]+)?$ $1.php?currentpage=$2
# Rewrite article URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ articles.php?article=$2
# Remove file extension from PHP files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L,QSA]
</IfModule>
Try adding the following to the .htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
#add these next 2 lines if you need to redirect http://www.domain.com/index.php?page=PAGE_NAME to http://www.domain.com/PAGE_NAME
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=([^\ ]+) [NC]
RewriteRule ^ %1? [L,R=301]
#send request to index.php
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1 [L,NC]