multiple htaccess rules - regex

I am struggling since last couple of hours to get it worked but no luck.. if any help will really be great.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteRule ^(.+)/$ /$1 [R=301,L]
RewriteRule ^profile/(.*)$ details.php?profile=$1 [L,NC,QSA]
RewriteRule ^industry/(.*)$ category.php?industry=$1 [L,NC,QSA]
RewriteRule ^page/(.+)$ index.php?page=$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [R=301,L]
very complex is it? but really not it is easy but when i am trying to add one more rule in above set of rules it is not working.
actual url - http://www.examples.com/req-info.php?tag=denis-reach-home&about=denis-reach&info=home
To URL - http://www.examples.com/tag/denis-reach-home/denis-reach/home
i tried varies things but no luck.. e.g -
RewriteRule ^tag/([^/]*)/([^/]*)$ /req-info.php?tag=$1&about=$2info=$3 [L]
any expert can share some tips.
thank you in advance.

You're very close. Have this code in your .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteRule ^(.+)/$ /$1 [R=301,L]
RewriteRule ^profile/(.*)$ details.php?profile=$1 [L,NC,QSA]
RewriteRule ^industry/(.*)$ category.php?industry=$1 [L,NC,QSA]
RewriteRule ^page/(.+)$ index.php?page=$1 [L,NC,QSA]
# your new rule
RewriteRule ^tag/([^/]+)/([^/]+)/([^/]+)/?$ req-info.php?tag=$1&about=$2info=$3 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [R=301,L]

Related

www to non-www with removing extensions htaccess

i've removed .php extensions first on my website. then i've forwarded www to non-www version. but there is a problem with that.
my .htaccess file looks like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^188\.166\.104\.194
RewriteRule (.*) http://example.com/$1 [R=301,L]
www forwards to non-www with that .htaccess. that is cool. but the problem is with the other files.
i'm now using: http://example.com/contact instead of: http://example.com/contact.php
but when you try to open http://www.example.com/contact that .htaccess forwards me to http://example.com/contact.php/
how do i fix that?
have a nice day!
Change the order of rules and some refactoring:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.example\.com|188\.166\.104\.194)$ [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteRule ^([^/]+)/$ $1.php [L]
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php [L]
Make sure to test is after clearing your browser cache.

htaccess url re-styling image url to seo friendly

I am currently using htaccess to force to use index.php file. like given below.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [L]
In current website it has some uploaded contents like
uploads/item/11.jpg
uploads/item/12.jpg
uploads/item/13.jpg
I wanted to re re-write it like.
uploads/item/11.jpg => uploads/item/11-itemName11whatEver.jpg
uploads/item/12.jpg => uploads/item/12-itemName12whatEver.jpg
uploads/item/13.jpg => uploads/item/13-itemName13whatEver.jpg
Can anyone help me to find RewriteCond and RewriteRule for this.
Updated
Updated htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} uploads-
RewriteRule ^uploads-([^/]+)-([0-9]+)-[^.]+\.([a-z]+)$ uploads/$1/$2.$3 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [L]
This helps me to make the URL look like
www.store45.loc/uploads-item-11-sample-watch.jpg
RewriteCond %{REQUEST_FILENAME} uploads-
RewriteRule ^uploads-([^/]+)-([0-9]+)-[^.]+\.([a-z]+)$ uploads/$1/$2.$3 [L,NC]

hide folder using .htaccess with subfolders

I have been trying to figure out this for a while but no success-
I have this site structure http://example.com/catalog/current/sub-folders/..
The result should hide the folder "current" so that the paths look like http://example.com/catalog/sub-folders/
This is what I have so far-
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+current/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^current/)^(.*)$ /current/$1 [L,NC]
when I place this .htaccess to the root and go to http://example.com/catalog/sub-folders/, it try to look for /current/catalog/sub-folders/
Any help to approach this problem will be highly appreciated.
Keep your code like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+(catalog)/current/(\S*) [NC]
RewriteRule ^ %1/%2 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(catalog)/((?!current/).*)$ $1/current/$2 [L,NC]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.+)$ $1.php [L]
Try making the last rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^hsc-bulletin/)^(.*)$ /current/$1 [L,NC]
This instead:
RewriteCond %{REQUEST_URI} !^/current/
RewriteRule ^(?!hsc-bulletin/)(.*)$ /current/$1 [L,NC]

.htaccess internal redirect not working

I tried doing some .htaccess redirects for internal pages but they are not working for me. This is my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteRule articles articles/how-to-play-piano [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>
I also tried this outside <IfModule> but not working:
Redirect 301 http://www.domain.com/articles http://www.domain.com/articles/how-to-play-piano
and
Redirect 301 /articles http://www.domain.com/articles/how-to-play-piano
Your regex is wrong for articles rule and will cause infinite looping. To fix that you need to use anchors ^ and $:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteRule ^articles/?$ articles/how-to-play-piano [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]

redirecting url containing specific string

I'm trying to redirect all urls that contain a specific string. The urls look like this
http://www.domain/com/modules.php?name=Kalender&op=list&d=8&m=6&y=2034
I have to redirect all urls that contain
name=Kalender&
to
http://www.domain.com/kalender/
I tried several rules in my .htaccess. None of them worked:
RewriteCond %{REQUEST_URI} name=Kalender&
RewriteRule .* kalender
RewriteRule ^(.)name=Kalender&(.)$ http://www.domain.com/kalender/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/name=Kalender&/i$ http://www.domain.com/kalender/ [NE,L]
RewriteCond %{REQUEST_URI} name=Kalender&
RewriteRule ^(.+)$ http://www.domain.com/kalender/ [L,R=301]
RewriteCond %{QUERY_STRING} ^name=Kalender&
RewriteRule ^name=Kalender& http://www.domain.com/kalender/ [R=301,L]
RewriteRule ^(.*)name=Kalender&(.*)$ http://www.domain.com/kalender/ [L,R=301]
RewriteCond %{REQUEST_URI} ^/modules.php?name=Kalender&$
RewriteRule ^(.*) http://www.domain.com/kalender [R=301,L]
This is the WordPress .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Any help would be appreciated!
You can use this rule as your first rule:
RewriteCond %{QUERY_STRING} (^|&)name=Kalender(&|$) [NC]
RewriteRule ^ /kalender/? [L,R]