RewriteEngine Adds relative path - .htaccess

I have such rewrite rules
RewriteEngine On
RewriteCond %{HTTP_HOST} ^monkey.pl(.*) [NC]
RewriteRule ^(.*)$ http://www.monkey.pl/$1 [R=301,L]
RewriteRule ^horse.html$ /dog.html
and when I go to the monkey.pl/horse.html I get the message:
The requested URL /home/login/monkey/dog.html was not found on this server.
How can I get this to work. Basically what I'm trying to do is to change address of urls like:
http://www.monkey.pl/produkty.php?strona=1
to be displayed as
http://www.monkey.pl/produkty/czesci_do_mixokretow.html
but none of my rules are working. Therefore I'm trying to come with solution.
I tried many varations and I couldn't get it to work. I don't want to rewrite whole page. Just 6 pages which I need to change url and that's all. Fixed translation url => url.

If you are only doing a handful of URLs then you can do them this way.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} /+produkty\.php\?strona=1 [NC]
RewriteRule ^ /produkty/czesci_do_mixo‌​kretow\.html [R=302,L]
RewriteRule ^produkty/czesci_do_mixo‌​kretow\.html$ /produkty.php?strona=1 [L]
RewriteRule ^horse\.html$ /dog.html [L]

Related

Removing id/variable from SEO friendly URL

I am trying to rewrite a URL so that it is SEO friendly and excludes the product ID. Currently I am using this in my .htaccess file:
RewriteRule ^shop/product/([^/\.]+)/([^/\.]+)$ products.php?id=$2&name=$1 [NC,L]
For example rewrites it to:
shop/product/product-name/12
Is there a way that I can rewrite this URL so it removes or excludes the product ID from the end?
Could you please try following, written and tested with shown samples. Please make sure you clear your browser cache before testing your URLs.
First set of Rules is considering that your product.php is present in root, in same path as .htaccess is present.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^shop/product/([^/]*)/([^/]*)/?$ /products.php?id=$2&name=$1 [NE,R=301,NC]
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{THE_REQUEST} \s/products\.php\?id=[^&]*&name=[^\s]*\s [NC]
RewriteRule ^(.*)$ shop/product [R=301,QSD,NE,L]
OR Please use either above OR following rules at a time only in your .htaccess.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^shop/product/([^/]*)/([^/]*)/?$ products.php?id=$2&name=$1 [NE,R=301,NC]
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{THE_REQUEST} products\.php\?id=[^&]*&name=[^\s]*\s [NC]
RewriteRule ^(.*)$ shop/product [R=301,QSD,NE,L]

Clean URLs in a subfolder

I've read and followed guides and looked for answers to other people's questions, but I'm struggling with url rewriting and htaccess.
I have a subfolder on my site with an index page which handles query strings to bring up dynamic content. I'd like the variable to appear to be a subfolder name e.g.
http://mywebsite.com/subfolder/index.php?v=variable
to
http://mywebsite.com/subfolder/variable
The .htaccess file I've made is at http://mywebsite.com/subfolder/ i.e. the same folder as index.php
How can I get this to work? Any help gratefully appreciated.
You can use these rules inside your /subfolder/.htaccess
RewriteEngine On
RewriteBase /subfolder/
RewriteCond %{THE_REQUEST} \s/subfolder/index\.php\?v=([^&\s]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ index.php?v=$1 [L]
Update (passing two values)
RewriteEngine On
RewriteBase /subfolder/
RewriteCond %{THE_REQUEST} \s/subfolder/index\.php\?v=([^&\s]+)&v2=([^&\s]+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteCond %{THE_REQUEST} \s/subfolder/index\.php\?v=([^&\s]+)\s [NC]
RewriteRule ^ %1? [R=301,L]
# Don't touch to existing files/folders
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Rewrite /xxx to /index.php?v=xxx
RewriteRule ^([^/]+)$ index.php?v=$1 [L]
# Rewrite /xxx/yyy to /index.php?v=xxx&v2=yyy
RewriteRule ^([^/]+)/([^/]+)$ index.php?v=$1&v2=$2 [L]
Use this in your .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} v=(.+)$ [NC]
RewriteRule ^ subfolder/%1? [R=301,L,NE]
This grabs the variable using %{QUERY_STRING} and then appends it to the rewrite using %1. You'll see I've added a ? onto the end of the rewrite. That is to stop the original query from appearing on the end of the URL.
I've used R=301 which is a permanent redirect. You might want to changes this to R=302 while you're testing, as this is temporary.
You can view a test of this rule working here: https://htaccess.madewithlove.be?share=7b6832e9-2c05-5d1d-916c-e4dd0f5b1da6
Make sure you clear your cache before testing this.

Using Htaccess to Remove Part Of The URL

I need to remove the /design/ from this URL (and any that contain it).
https://www.domain.com/design/subcategory/productname1
At the end, I want it to look like this:
https://www.domain.com/subcategory/productname1
But since that would obvious cause a 404 of you just deleted that part of the URL, I need to populate that second URL with the contents of the first. I apologize if I am not using the correct terms but here is an example of something similar I have on my site.
In my header menu, I have a link that indicates it will take you here:
https://www.domain.com/index.php?route=product/search&tag=shirts
But I wanted it to look like this:
https://www.domain.com/shirts/
So I researched it and this solution worked great:
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+product/search/\?tag=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ product/search/?tag=$1 [L,QSA]
Can something similar be done to remove the /design/ part of the URL?
Thanks!
Try these rules:
RewriteEngine On
# remove /design/ from URLs and redirect
RewriteCond %{THE_REQUEST} \s/+design/(\S*)\s [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# internally add design/ to know set of URIs
RewriteRule ^(skulls-bones|characters|abstract|animals|games|geek-nerd|graphic|keep-calm|movies-tv|pop-culture|tattoo|typography)(/.*)?$ design/$1$2 [L,NC]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+product/search/\?tag=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/design/ [NC]
RewriteRule ^([^/]+)/?$ product/search/?tag=$1 [L,QSA]

Htaccess possible 301 redirect

I have a question about htaccess.
I have a url like this :
http://www.asd.com/producten/kia?category=car
And I want this to be :
http://www.asd.com/producten/car/kia
Is that possible with htaccess?
I have tried different ways to solve it but so far no luck.
I've tried:
RewriteRule ^producten/([^/]+)/([^/]+) /producten/$2?category=$1 [NC]
AND
RewriteRule /producten/(.*)/(.*) /producten/$2?category=$1 [R]
Thanks in advance,
Mert
Try using the following in your /.htaccess file:
RewriteEngine On
# Step 1: Redirect the old URI to the new one and prevent looping
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} category=([^\s&]+) [NC]
RewriteRule ^(producten)/([^/]+)/? /$1/%1/$2? [R=302,L,NE]
# Step 2: Internally rewrite the new URI to the old one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(producten)/([^/]+)/([^/]+)/?$ /$1/$3?category=$2 [L,QSA]
If you would like to make the redirect permanent, change R=302 to R=301.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /(producten)/([^?]+)\?category=([^\s&]+) [NC]
RewriteRule ^ /%1/%3/%2? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^(producten)/([^/]+)/([^/]+)/?$ $1/$3?category=$2 [L,QSA,NC]

404 redirect works but changes rewritten URL path in browser to full URL path

I use a URL rewriting scheme of the following:
example.com/about/
example.com/this/is/a/page/
GOES TO:
example.com/pages/about/about.php
example.com/pages/this/is/a/page/page.php
It works fine, but on a 404 error, when typing in example.com/badpage/ it shows 404 page but changes the URL string to example.com/pages/badpage/badpage.php.
How to do keep the URL the same even on a 404 error?
(The htaccess also adds on '/' to the end of the URL requested as you can see from the code below)
htaccesss code:
DirectoryIndex /pages/index/index.php
ErrorDocument 404 /pages/error/404.php
RewriteEngine On
#Removes the www from domain for SEO
RewriteCond %{HTTP_HOST} ^www\.portal\.example\.com$ [NC]
RewriteRule ^(.*)$ http://portal.example.com/$1 [L,R=301]
# Don't fix direct file links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://portal.example.com/$1/ [L,R=301]
RewriteRule ^([A-Za-z0-9_-]+)/?$ pages/$1/$1.php [NC,L]
RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/?$ pages/$1/$2/$2.php [NC,L]
RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/?$ pages/$1/$2/$3/$3.php [NC,L]
RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/?$ pages/$1/$2/$3/$4/$4.php [NC,L]
You need to add conditions before each of your rewrites to ensure you're not blindly rewriting into a file that doesn't exist. Mainly, the 4 rules at the end need to have some conditions:
RewriteCond %{REQUEST_URI} ^/([A-Za-z0-9_-]+)/?$
RewriteCond %{DOCUMENT_ROOT}/pages/%1/%1.php -f
RewriteRule ^ pages/%1/%1.php [NC,L]
The first condition creates a grouping which is backreferenced using %1. The second condition creates a path that you are trying to rewrite to and checks if that file exists using -f. Same thing for the others:
RewriteCond %{REQUEST_URI} ^/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/?$
RewriteCond %{DOCUMENT_ROOT}/pages/%1/%2/%2.php -f
RewriteRule ^ pages/%1/%2/%2.php [NC,L]
RewriteCond %{REQUEST_URI} ^/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/?$
RewriteCond %{DOCUMENT_ROOT}/pages/%1/%2/%3/%3.php -f
RewriteRule ^ pages/%1/%2/%3/%3.php [NC,L]
RewriteCond %{REQUEST_URI} ^/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/?$
RewriteCond %{DOCUMENT_ROOT}/pages/%1/%2/%3/%4/%4.php -f
RewriteRule ^ pages/%1/%2/%3/%4/%4.php [NC,L]

Resources