I am creating a website and cannot figure out how to set clean URLs throughout all my webpages. Is there a way to do this without .htaccess? If no how can I accomplish with it? I'm using Apache.
This is an example of what I have now, which I do not want:
www.example.com/about.html
This is what I want the URL to look like:
www.example.com/about/
For .html you can code below, it will rewrite request when you .html is enter in url.
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]
Unless your Apache is configured differently, the file /about/index.html will be returned under the URL /about/.
By default, Apache looks for a file named index.html, but this can be changed with the DirectoryIndex directive.
You must create the file .htaccess in your server and add this code in it:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]
Related
In my site I had a desire to make simpler URLs such as:
http://www.example.com/about
Instead of:
http://www.example.com/about.php
Mostly for aesthetics, for now I have Apache redirect to ./default.php if a directory is requested. However this forces me to create a directory at
http://www.example.com/about/ and a file inside it called default.php ending up with:
http://www.example.com/about/default.php
I know there's a better way, probably using PHP or JS, what do?
it's all in apache's configs,
rewriteEngine can do that for you
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/
A snippet taken from third link:
To remove the .php extension from a PHP file for example yoursite.com/wallpaper.php to yoursite.com/wallpaper you have to add the following code to your Apache config:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
If you want to remove the .html extension:
RewriteRule ^([^\.]+)$ $1.html [NC,L]
That’s it! You can even link pages inside the HTML document without needing to add the extension of the page. For example:
wallpaper
I am working with WAMP server. This is my filename ("localhost/blogs.php")
but I want to call this "blog.php" file as ("localhost/blogs"). How can I remove the file extension from the URL?
If you are using an apache server the easiest way is to use mod_rewrites
Create a .htaccess file and write the following code:
# Options
Options -Multiviews
Options +FollowSymLinks
#Enable mod rewrite
RewriteEngine On
RewriteRule ^blog$ blog.php [QSA,L]
This will mean when /blog is added in the browser then the server will call blog.php
That's not a PHP issue but a mod_rewrite one. Enable mod_rewrite in Apache (probably already the case) and then add a file named .htaccess in your website's root directory and add this content to the .htaccess file:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
PHP frameworks use this kind of thing, I can advice codeIgniter.
Otherwise you need to create a URL handler, and use a .htacces file
This might help
i am coding a PHP website, and now want to hide the PHP file type in URL. for example use
http://.../signup
replace the URL
http://.../signup.php
how should i do in my apache configure file?
You can use this rule to append .php if the requested path with that prefix maps onto an existing file:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php
Now all you need to do is to use the paths without the trailing .php in your documents.
didn't try it, but this should work:
RewriteEngine on
# Rewrite /foo/bar to /foo/bar.php
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]
How to get file directory trough .htaccess by using RewriteRule ^(.*)$ ?id=$1 [L,QSA]?
If .htaccess is located in http://localhost/some/dir/.htaccess and I'm opening http://localhost/some/dir/here/I/use/RewriteRule/, how I detect value /some/dir/ without using RewriteBase and without manual adding %{DOCUMENT_ROOT}/some/dir/, like value localhost I get trough %{HTTP_HOST}?
If you do not use RewriteBase you need to tell mod-rewrite the real Directory Root /var/ww/mysite/some/dir in the rewrite rule. RewriteBase would take the location url and map it to the directory.
So you'll maybe end up with
RewriteRule /var/ww/mysite/some/dir/(.*)$ ?id=$1 [L,QSA]
And trying to map some internal variables it may be
RewriteRule %{DOCUMENT_ROOT}/some/dir/(.*)$ ?id=$1 [L,QSA]
But I'm unsure, I rarely use mod_rewrite in .htaccess -- I prefer Directory tags, and the file path management can be different in .htaccess (auto removal and adding of directory prefixes). If you do not find a solution try to ask Servfault, plenty of admins other there.
Actualy Apache still does not have pathinfo($,PATHINFO_DIRNAME), function like has PHP.
So on now there are solution on using %{REQUEST_URI}, like this example:
RewriteRule ^(.+)/$ /path-dirname/$1 [R=301,L]
may reset with:
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^.+/$ %1 [R=301,L]
I'm using the Zend Framework, so I'm bootstrapping into a file called index.php. Naturally, I don't want images to be bootstrapped, so I've added a .htaccess file. Here's what it looks like
/application
/library
/public (this is the root of the site)
/images
/js
.htaccess
index.php
This is what's written in my .htaccess:
RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|mp3|mov|css)$ index.php
This is basically saying if it doesn't end in .js / .ico / etc, then send the request to index.php. It works just fine on my localhost, but when I get up to actually putting it online, it doesn't. It just routes everything to index.php, regardless of the ending of the request. When mywebsite.com/images/wizard.gif should just show the picture, it tries to load the images controller, which is not what I want it to do.
What could be going wrong? I know it's reading the .htaccess. Is it reading my regex wrong? Why would one apache server read it wrong, while another reads it correctly? Any help would be great.
Here is my .htaccess if you would like to give it a try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
Basically it checks to make sure the request is not a directory, symlink or a real file, and then sends it to index.php. Otherwise it will provide direct access to the file/directory