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)
Related
I'm trying to rewrite my URL from request/2 to request?number=2
I tried to write this in my .htaccess
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^request/(.*)$ request?number=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
But the result is a 404 Error
Can someone help me?
With your shown samples, could you please try following. Please do clear your browser cache before testing your URLs.
RewriteEngine ON
##To remove .php extension eg--> someone hits http://localhost:80/test.php it will redirect to http://localhost:80/test
RewriteCond %{THE_REQUEST} \s([^.]*)\.php\s [NC]
RewriteRule ^ %1 [R=301]
#RewriteRule ^(.*)$ $1.php [L]
##All non-existing files/directories will come in this rule whose URI starts from request.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^request/([\w-]+)/?$ request.php?number=$1 [NC,L,QSA]
##All non-existing files/directories will come in this rule set apart from URI which starts from request which is taken care above.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I've got the following .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^profile/([A-Za-z]+) profile.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
But you can't add a / to the end of the URL. For example if you goto something it works but something/ doesn't. So how do I allow a / to be added to the URL?
Here is a possible solution if anyone else is having the same problem (not fully tested but seems to work).
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^profile/([A-Za-z]+) profile.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*?)(/.*)?$ $1.php [NC,L]
To allow the trailing slash in URLs that point to your php files, replace your last rule with this :
RewriteRule ^(.*)/?$ $1.php [NC,L]
/? matches the slash as an optional character so your rule will also match URIs not ending with a / .
I did a php blog and I would like to have friendly SEO urls for my posts, and also remove .php extension from my urls. So I edited my htaccess file like this :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [N]
#second condition and rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
So I got my friendly SEO post urls but the second condition doesn't work. But it works when I use it as my first condition. What I did wrong ? Thanks a lot for your help
Keep first rule as .php handler rule but that should check for presence of .php file as below:
RewriteEngine On
# if not a directory and file.php exists then rewrite file to file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# rewrite every non-file or non-directory to viewpost.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ viewpost.php?id=$1 [L,QSA]
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]
I would like to redirect all paths like this:
myurl.com/worldwide/en
myurl.com/worldwide/pt
myurl.com/worldwide/de
to:
myurl.com/worldwide/index.php?lang=en
myurl.com/worldwide/index.php?lang=pt
myurl.com/worldwide/index.php?lang=de
Just to be clear a dynamic redirection of the pathname after /worldwide
Actually ideally I would like to keep the original url (e.g. myurl.com/worldwide/de) but load the same php file with the language directory as a param but not sure if this is possible?
Thanks
Use this code
RewriteEngine On
RewriteBase /worldwide/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?lang=$1 [L,QSA]
Please let me know if this helps you
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php !-f
RewriteRule ^(.+)$ /index.php?page=$1 [L,QSA]