".htaccess" file not working at all - .htaccess

I have this code in my htaccess file
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}\.php !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC]
This help to remove the .php extension in my url. but now I've been trying for weeks to rename a URL I have.
article.php?num=5 to article/num/5 or maybe the title of the article it self nothing has worked so far. this is what I tried using.
RewriteRule ^article/([0-9].+)/?$ article?num=$1 [NC]
Yet I always get internal server error i really need help with this its been frustrating.

Your RewritePattern doesnt accept the /num/ segment, you need to adjust it so it matches your your uri /article/num/digits . Change your pattern's regex to this
^article/num/([0-9]+)$

Try it like below rule,
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([\w-]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/num/([\d]+)$ $1.php?num=$1 [QSA,L]

Related

2 Rewrite Rules for 2 Conditions?

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . Core.php
Now I have this above but, would like to know how/if I can mix these into one.
I'm new to this .htaccess so if you could give me a link or somewhere to learn that would be great. ;)
I also couldn't find an awnser that worked on google searches or anything explain how it works so I can configure it to work for me. :L
You can use :
#Rewrite /file to /file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
# Rewrite any other request to /core.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /core.php [NC,L]
Explaination :
RewriteCond %{REQUEST_FILENAME}.php -f
The line above checks to ensure that the URI.php is a file, if it is an existent php file, then
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
The rule rewrites the request to its orignal location request.php (file => file.php)

RewriteRule for Pretty URLs not working

The URLs of my site are of the type
http://localhost/cms2/pages.php?mpage=2
I have written the following .htaccess in order to create pretty URLs, but nothing happens
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9]+)\/$ pages.php?spage=$1 [NC]
Mod-rewite is ennabled and other rewrite rules I've tested work. For example, I have a file called "contact-us.php". I can make it look "mysite.com/contact-us" using the following code:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^(.*)\.html$ $1.php [nc]
Any help would be appreciated.
Thanks in advance.
Sonia
Not working because your regex is incorrect. Use this rule in your root .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9]+)/([^./]+)/?$ /cms2/$2.php?spage=$1 [L,QSA]
This will rewrite a pretty URI: /3/page/ to /cms2/page.php?spage=3

Set language using htaccess URLrewrite

I have for example two files in my root directory: one.php and two.php. I would like to reach them through these urls without having to actually have the physical directories en+de.
mydomain.com/en/one
mydomain.com/en/two
mydomain.com/de/one
mydomain.com/de/two
So the first directory would be "ignored" and just pass a GET variable with the language setting to the php-file, second directory is the php-file without extension.
This is what I'm using in my htaccess to loose the file extension so far:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
UPDATE/SOLUTION
This is quite useful: .htaccess rule for language detection
and this seems to pretty much do what I wanted:
RewriteRule ^(en|de|fr)/(.*)$ $2?lang=$1 [L,QSA]
RewriteRule ^(.*)$ $1?lang=en [L,QSA]
ONE MORE ADDITIONAL QUESTION
For mydomain.com/en/one/id45
If I wanna pass id45 as a variable to one.php what line in htaccess do I have to add?
You can try this rule:
DirectoryIndex index.php index.html
DirectorySlash On
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$2.php -f
RewriteRule ^(en|de|fr)/([^/]+)(?:/(.*)|)$ $2.php?lang=$1&var=$3 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]{2})/?$ index.php?lang=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ $1?lang=en [L,QSA]

Removing the extension won't work

I've searched a whole lot to get this done.
I've found many solutions which people have claimed to be working.
But it's just not working for me. And I've got no idea why that is.
Below is my .htaccess as it is right now.
The first 4 lines of code are working correctly, the rest isn't.
Also, do I need to change the links in my navigation aswell?
For the website click here.
CODE
Options Includes FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^dylanvanheugten.nl$ [NC]
RewriteRule ^(.*)$ http://dylanvanheugten.nl/$1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
# 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]
Can anyone please help me fix my file?
Thanks in advance!
And one last thing, please explain what you are doing. ;-)
This condition:
RewriteCond %{REQUEST_FILENAME}\.php -f
will fail because you have a trailing slash. For example, if you request /foo/bar/ expecting to get served the contents at /foo/bar.php, the trailing slash would make this condition check that /foo/bar/.php exists as a file, which doesn't.
You need to tailor your check to ignore the trailing slash:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^/]+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([^/]+)/$ $1.php
The other thing you are missing is an external redirect for requests made for php files:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^/]+)\.php
RewriteRule ^ /%2/ [L,R=301]

hide file name extensions in url?

can some help me please i am wanting to hide the file name extension in the url of my site.
so an example would be
http://www.mysite.com/home.php
http://www.mysite.com/control.php
would instead show as
http://www.mysite.com/home
http://www.mysite.com/control
i have been looking into this and found this code which seems to hide the file name extension in the url but now my links wont work and im getting a 404 error. i think that i need to amend my site link paths or something like that.
can someone show me what i need to do please, here's the code im using in my .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.]+\.php(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php$ http://www.mysite.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ http://www.mysite.com/$1/ [R=301,L]
If you want to hide all the extensions (not just .php, .html) then use:
Options +FollowSymLinks +MultiViews
in your httpd.conf or .htaccess
Google can help before stack :)
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]
http://eisabainyo.net/weblog/2007/08/19/removing-file-extension-via-htaccess/
Also lookat
Making a rewriterule remove .php extension?

Resources