removing /index from the url on htaccess - php

My current urls look like this [mysite].com/index
I removed .php's with the following .htaccess code but i can not remove the index from the url.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>
How can i remove 'index' from the url?
EDIT
I found why it does not work because of windows hostings does not support htaccess method.

Use
DirectoryIndex index.php
Now apache will treat index.php as directory index and you don't have to type index or index.php in url bar.

Actually, removing the page name for good it's not a good practice.
It may cause errors, and it's not good for SEO.
I strong suggest you to use friendly URL to remove the PHP extension(the way you're doing right now) or put some other name more friendly.
Exemple:
RewriteEngine On
RewriteRule ^home/?$ /my_new_index.php [NC,L]
RewriteRule ^new-order/?$ /new_order_from_client.php [NC,L]
It's the best practice!

Related

.htaccess - Redirect

What I want is to redirect by .htaccess
http://example.com/omgitsaname
I want really to redirect, or better, to stay the same but to get the information from
http://example.com/product?id=omgitsaname
If you want to use these types of URL's in your browser
http://example.com/omgitsaname
You can do it this way and make sure the that the URI is not a real directory or a file.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /product?id=$1 [NC,L]
Here is how you can use Rewrite Rules via .htaccess, in Apache
RewriteEngine On
RewriteRule ^product/?$ do/something/here [NC,L]
RewriteRule ^([a-z]+)/?$ product?id=$1 [NC,L]
Make sure the mod_rewrite module (or equivalent) is enabled.
Also add the following rule: AllowOverride all (or equivalent) to allow .htaccess to work.
Note: Modify the regular expression as per your needs.
Edit: Thanks to #4sha to fix the redirect loop.

How to set default homepage from another folder in .HTACCESS file?

I created a site and make its URL seo friendly like www.mywebsite.com/home.
but actual homepage is in my /SomeFolder/index.php which is rewrite its url to /home.
Now i want to set /home url as a default page in htaccess file. But when i set this.
DirectoryIndex /home
I got the 500 error. One way is to give the redirection in index.php so it will redirect me to the /home page. but i think its a weird method.
Is there any other way??
Thanks
Remove that DirectoryIndex line.
You'll need this rule in your DocumentRoot/.htaccess:
RewriteEngine on
RewriteRule ^home/?$ /SomeFolder/index.php [L,NC]
this best article for create pretty URLs :
http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/
Try DirectoryIndex home/index.php
use this one , hope this will helpful for you
Options +FollowSymLinks
RewriteEngine on
RewriteRule home(.*)\$ index.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} !^/somefolder
RewriteRule ^(.*)$ /somefolder/$1 [L]
src: https://www.siteground.com/kb/how_to_change_my_document_root_folder_using_an_htaccess_file/
Although I have entered an issue so I have an open question, which might be worth monitoring. I will update if I figure it out myself which I aim to do.
.htaccess make sub dir home dir but force www and https whilst maintaining links

.htaccess being ignored? Not redirecting like it's supposed to

Okay I'm trying to use Lando (landocms.com) and I'm trying to get the pretty urls option to work.
Basically by default Lando creates link like: domain.com/index.php/page. Supposedly, there is a way to remove the index.php so the links become: domain.com/page. I have created an .htaccess as directed, however it does not work.
Here is the .htaccess I am using:
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I have tried alot of variations, /index.php/, index.php? and plenty more but none work. According to HostGator everything should be fine. Any thoughts? I think I'm going crazy haha.
Thanks!
Rewriting for a CMS is a two-tier approach. First, you need to set your .htaccess (I have put a safer one here for you):
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .+ index.php [QSA,L]
Then, LandoCMS allows you to remove the index.php from the generated addresses, by means of turning on the appropriate setting in the administration panel. See this link for more information.
If the .htaccess content I've given you doesn't work, then simply use the one that the CMS has given you.
You want to remove the index.php part from any URL, but process the incoming, friendly URLs through index.php nevertheless
RewriteEngine On
# remove index.php and redirect client
RewriteCond %{ENV:REDIRECT_SEO} ^$
RewriteRule ^/?index.php/(.*) /$1 [R,L]
# process friendly URL
RewriteCond %{REQUEST_URI} !^/index.php/
RewriteRule .+ /index.php/$0 [E=SEO:1,L]
The environment setting E=SEO:1 prevents an endless loop.

url rewriting index.php

i have urls like
http://mysite.com/index.php?p=resources
http://mysite.com/index.php?p=resources&s=view&id=938
but i want urls like
http://mysite.com/resources
http://mysite.com/resources/view/938
instead of making hundreds of rewrite rules i wonder if it would be possible to just have one? Ive head this is possible by "getting the uri and splitting it into parts" and then just add a rewrite rule for index.php
but how? could someone give an example or link a tutorial
I happen to use this in .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
This essentially calls index.php no matter what is requested. Then inside your PHP code you can look at $_SERVER['REQUEST_URI'] to get the URL and parse it accordingly.
The RewriteCond lines are there to exclude direct calls to files. In my case I don't want stuff like requests for js/css/image files to go through index.php for performance reasons.
Create a .htaccess file. Not somefilename.htaccess, it is simply named .htaccess.
Note: Your index.php and .htaccess file should be in the same directory.
Now try this on your .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([A-Za-z0-9\-]+)$ index.php?p=resources
RewriteRule ^([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+)/([0-9]+)$ index.php?p=resources&s=view&id=938
</IfModule>
see more about url rewrite here

.htaccess rewriting url to page or directory

For my site I have a RewriteRule that points the URL http://www.mysite.com/work to a work.php file. I also have a directory called "work" that has files in it, like project1.php, project2.php, etc...
What rules would I have to write so that the URL http://www.mysite.com/work knows to go to the work.php file, but the URL http://www.mysite.com/work/project1 knows I mean to go inside the directory "work" and display the project1.php file?
EDIT: Should point out, this is what I'm currently working with:
RewriteEngine On
RewriteBase /beta/
RewriteRule ^([a-z]+)$ $1.php [L]
Any additional tips to improve this security-wise? (Stopping directory jumping, etc...)
Try this:
RewriteEngin On
RewriteBase /
RewriteRule ^work$ /work.php [QSA,L]
That will ensure that http://www.mysite.com/work (no trailing slash) will go to your work.php file.
If you also want http://www.mysite.com/work/ (with trailing slash) to go work.php, add this line just above the last RewriteRule.
RewriteRule ^work/$ /work [R=301,QSA,L]
That will redirect it to the URL with no trailing slash thus, displaying the work.php file.
UPDATE: Since you already have a RewriteBase directive, just put the RewriteRule line(s) right after your RewriteBase but before your RewriteRule as the rule you're using a catch-all and will match everything.
This is the answer to your question. It works for me.
RewriteEngine On
RewriteBase /
RewriteRule ^([a-z]+)$ $1.php [L]
Simply remove beta/
Try this rule:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .* $0.php [L]

Categories