Rewrite rule to remove file extension from sub-directory - php

I want to remove the extension of file which is in subdirectory i.e. abc.com/subdirectory/file.html, tried evry possible way with rewrite rule, but nothing seems to work.

I think you want something like
http://www.abc.com/subdir/file right?
So you can do it with .htaccess with
## Remove Extension ##
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
I use it with php and then I simply write www.abc.com/subdire/file
*REMEBER * I don't know if this solution suit all your needs, your question is really general. Please provide some information

This code will work for you.
URL 'domain.com/index.html' to display as 'domain.com/index'
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

Related

how to change url rewrite by .htacces with .php extention

my website is,
http://localhost/mywebsite/page.php?id=123
using .htaccess i changes my url like this
http://localhost/mywebsite/newpostof2016
but i want like this final url
localhost/mywebsite/newpostof2016.php
current using .htacces code is
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9]+)$ page.php?id=$1
does this work? It should add .php to all files even if they arent php files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [L]
N.B. there are many reasons as to why you wouldn't want this behavior. I can't think of a case where this would benefit UX
This rule should do it.
RewriteEngine On
RewriteRule ^mywebsite/newpostof2016\.php$ /mywebsite/page.php?id=123 [L]

Mod Rewrite Logic

I never really have used mod re write and I am trying to understand the best way to implement it. removing the .php extension. i found this code
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
problem is that if the link is to domain.com/index.php it wont re write it domain.com/index
Also if i do the link domain.com/index the person can still add the .php and it still works fine.
I would like to know the proper way.. do i have to hard code the urls in the html or is there something that can do it automatically?
I dont want them to be able to add the .php to the end
You need an additional rule to remove .php . The rule you are using just allows you to access php files without their extension but doesn't remove the extension. To remove the .php you can use :
RewriteEngine on
#1) redirect "/file.php" to "/file"
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [L,R,NE]
#2) internally map "/file" back to "/file.php"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

htaccess to php with different name/variable combos

I have php that works awesome
But I wanna change it from showing variables to look like dirs
eg:
example.com/?mode=page&page=15 to example.com/page/15
and
example.com/?mode=pic&picid=136 to example.com/pic/136
however I only know of one way to do it, such as:
RewriteRule ^(.*)$ index.php?mode=$1&page [R]
but that only works for one case, otherwise it 404s
I tried using RewriteRule ^(.*)$ index.php?url=$1 [L,QSA] so that the whole path gets passed. However, this way it doesn't seem to pass the CSS in my pages.
I'm extremely confused...
Here's my htaccess file
# Do not remove this line, otherwise mod_rewrite rules will stop working
RewriteBase /
IndexIgnore *
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^css/styles\.css$
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
The problem is you are rewriting ALL urls to
index.php?url=
So your CSS files also get "redirected" to "index.php?url=" eg
index.php?url=css/styles.css
So you need to add "conditions" to your .htaccess so that certain files (or directories) dont get "redirected". You can do this by using "RewriteCond" (conditions) before your "RewriteRule ".
For example to not "redirect" the css file "css/styles.css" you could do
RewriteCond %{REQUEST_URI} !^css/styles\.css$
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]

Use directories in url as php variables

I am after a way of using the directories in the URL as PHP variables. Similar to Wordpress (if not all blogging platforms) I want a way to, say, pass domain.com/directory into a php file.
The reason for this is so that when I create my own blog things, the URLs will be SEO friendly.
Forexample, instead of
domain.com/?blog=1&foo=1&bar=1&foobar=5
I want
domain.com/1/1/1/5 or something
where I can then use the explode function to get variables.
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /?blog=$1&foo=$2&bar=$3&foobar=$4 [L]
There's a lot of generators on google, search it and stackoverflow about rewrite rules, you'll find a lot of info.
For that functionality in php, you need mod_rewrite or the equivalent in your webserver of choice, and to rewrite the url path into a get param instead.
Google "clean urls drupal", for an example of it in the wild.
RewriteEngine On
RewriteRule ^(.*)$ /index.php?url=$1 [L]
then in your index.php you can do explode, regular expresions or whatewer you want with $_GET['url']
or you can skip files that alredy exists like css or javascript and image files
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /index.php?url=$1 [NC,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/(.*)/(.*)/(.*)$ ?blog=$1&foo=$2&bar=$3&foobar=$4 [PT,L]
I think this ought to work for your case specifically. The (.*) groups one or more characters of any type separated by a /. In this case you'll get them as GETs with the keys e.g foo
Otherwise as a string for exploding:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ blog.php?string=$1 [PT,L]
Assuming there's a blog.php file in the same directory to process the string
Hope it helps

Link Duplicity problem with .htaccess and mod_rewrite

Sorry I couldn't come up with a better title.
Here is my htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L,QSA]
And this is my problem:
localhost/validpage gives me the contents of localhost/validpage.php.
localhost/validpage/blah also gives me the same (validpage.php) page and so does localhost/validpage/blah/blah/...
Therefore the problem I am facing is link duplicity(in my words!).
How do I allow localhost/validpage.php to be accessed from localhost/validpage only and nothing else, not even localhost/validpage.php.
I have started a question on *Server****Fault*** too but with not much success.
The answer I have got is it cannot be done with htaccess alone.
By validpage I mean any valid page on the server. Since I am retrofitting an existing site with mod_rewrite for cleaner urls, I am looking for a relatively easy solution preferably with .htaccess only. However, any solutions are welcome.
what is the source attribute of your images, etc ???
absolute or relative?
<img src="/images/my.jpg" /> and <img src="images/my.jpg" /> point to different files when applying your rewrite rules.
You could try using this in your .htaccess
RewriteEngine on
RewriteBase /
# if request has a php extension remove and redirect
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^((.*)\.php)$
RewriteRule ^(.*).php$ $1 [L,R=301]
# if request uri has no extension link to php file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I should rewrite your php scripts to friendly urls, and redirect requests using the .php extension.

Categories