Htaccess doesn´t work - nice url - .htaccess

Hi guys i have problem with a htaccess.
I want from this URL domain.com/index?article=28&title=velky-biel-karlova-ves
(it doesn´t have index.php because i hide .php extension with htaccess.
I wanna have this URL domain.com/clanok/28/velky-biel-karlova-ves
I used this code
My htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^clanok/([\d]+)/([\w-]+)/?$ index.php?article=$1&title=$2 [NC,L]
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
My index.php file:
<a href="index.php?article=<?php echo urlencode($row['id']); ?>&title=<?php echo createSlug($row['title']); ?>" title="">
It doesn´t work.
Thx for help.

Try it like this you still have to mention index.php your php hide rule is different from this,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^clanok/([\d]+)/([\w-]+)/?$ index.php?article=$1&title=$2 [NC,L]
Please ensure this rule is not conflicting with your existing rules.

Related

apply slash at the end of the URL with htaccess conditions

I need help here.
I want apply a slash at the end of my urls with htaccess, but unfortunately its not working this way, maybe you guys can help me here?
This is my htaccess:
RewriteEngine on
RewriteBase /
## hide .php extension except in POST method
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{REQUEST_METHOD} !POST [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
I tried add this rule, but do not work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
I appreciate any help here.
Thanks :)

Get Rewrite rules to be ignored on specific URL's

I have re-created a site from WordPress in native PHP, and have added certain ReWrite rules in the .htaccess to mimic the WP URL's, but I need to add a couple of reWrite rules to beautify a couple of URL's.
I tried creating 2 directories and having separate .htaccess files in each directory, but to no avail.
RewriteEngine On
RewriteBase /
# hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
#Beautify new URL's
RewriteRule ^operating-areas/([^/]+) town.php?place=$1
RewriteRule ^operating-area/([^/]+)/([^/]+) keyword.php?place=$1&keyword=$2
www.domain.com/operating-areas/London
needs to be sent to
myPage.php?place=London
and then
www.domain.com/operating-area/London/Finance
needs to be sent to
myOtherPage.php?place=London&keyword=Finance
At the moment I am getting 'Too many redirections'.
The rules I have used for the WordPress styling were found on the internet, hence my limited knowledge of this subject hinders me from stopping them firing in specific circumstances.
You can replace all of your code with this block:
RewriteEngine On
RewriteBase /
# hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
#Beautify new URL's
RewriteRule ^operating-areas/([^/]+)/?$ town.php?place=$1 [L,QSA,NC]
RewriteRule ^operating-area/([^/]+)/([^/]+)/?$ keyword.php?place=$1&keyword=$2 [L,QSA,NC]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
/?$ at the end of the pattern allows matching of an optional / at the end. It is also important to keep this rule before .php adding rule.

Remove the file extension .php using htaccess file

I want to remove the .php from the url through htaccess file. for example home.php to home
I'm using the following rewrite rule in htaccess file.
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]
I also want to assign the login as index.
How can I change it.
This is the code you can use to hide .php extension:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L,NC]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# Replace html with your file extension, eg: php, htm, asp
To add a trailing slash at the end of the URL
(for example: http://www.example.com/about-us/ to go to http://www.example.com/about-us.html)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

.htaccess .html redirects

I've been asked to set up a redirect on a website.
Currently the website redirects allows you to visit http://www.site.co.uk/test instead of /test.html
The code being used is:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
I was wondering if anyone knows a way to keep this behaviour, but also redirect the .html extension to the non html version?
Try adding the following to your htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
#redirect .html version
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ (/[^\ ]+)\.html\ [NC]
RewriteRule ^ %1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]

folder doesn't load while making the URLs work with and without the .php

I included the code for making the URLs work with and without .php. I included the below written code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Now the url works with and without .php. For e.g. if we put http://www.test.com/test.php or http://www.test.com/test both works. But the problem is folders doesn't load. For e.g. http://www.test.com/admin doesn't load. not found error is shown. Admin is a folder. Any idea?
Try this code for .php extension removal instead:
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s.+\.php [NC]
RewriteRule ^(.+)\.php$ /$1 [R=301,L,NC]
# To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule . %{REQUEST_URI}.php [L]
Make sure to comment out your existing code
Try below :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [NC,L]
this will also work with a directory path.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
just try this...I think its working

Resources