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
Related
Good day all, today a client is arrived with a already running Joomla! site, they have some custom content on this site and it has been done by using PHP code directly into some pages, the result is that there is a single Joomla! page, that when requested with a GET param is showing different content.
The URLs are something like this:
www.example.com/catalog/product-category/product-details.html?intid=1234&name=889-abc-456
what I'd like to obtain is to have URLs like:
www.example.com/catalog/product-category/product-details/1234/889-abc-456/
and I'd like to obtain this by editing the .htaccess file, to avoid touching the code of the site.
is that possible without braking everything considering that .htacces is been modified by Joomla! ?
actually, I've something like:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /product-details\.html\?intid=([^&]+)&name=([^&\ ]+)
RewriteRule ^ /product-details/%2/%3/? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?product-details/([^/]+)/([^/]+)/?$ /product-details.html?intid=$1&name=$2 [L]
Editing the htaccess file for Joomla URL is not a good idea. Instead of doing changes in htaccess which are more complicated you can take look at router file of the component. Check more details here https://docs.joomla.org/Supporting_SEF_URLs_in_your_component
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]
I recently added these lines to my .htaccess file on the server:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
What I was trying to do was eliminate the need for .php after the file name. This worked wonderfully, but something else was broken.
The URL for the website was http://subdomain.something.domain/eCom/ and I had an index.php file in it, which was automatically loaded when the above link was used. But after editing the .htaccess file, the above link returns a 404 error. The page still opens correctly if I use http://subdomain.something.domain/eCom/index or http://subdomain.something.domain/eCom/index.php. I don't know what's happening. Anyway this can be resolved?
Note: I don't understand the code above about all the RewriteEngine stuff. I just copied from a website hoping for a quick fix. Any additional information about it is welcomed, if that's what's breaking my code.
Add RewriteCond %{REQUEST_FILENAME} !-d above your first RewriteCond to avoid matching directories as well.
I'm setting up URL rewrite rules for an application I'm developing so that I can use nice clean URLs. I want the URLs to look like http://app.com/page/agency/ and to be equivalent to http://app.com/index.php?p=page&agency=agency. The agency selector is optional, so I want the URLs to redirect, even if the agency is not present. I have created the following mod_rewrite rules for this purpose:
RewriteRule ^/?([a-z]+)/$ /index.php?p=$1 [PT]
RewriteRule ^/?([a-z]+)/([a-z]+)/$ /index.php?p=$1&agency=$2 [PT]
This is working fine for redirecting the pages. However, it seems to me that my javascript files are being re-loaded with each page, as if the browser thinks that it's in a different directory and needs to re-load the JS files. The JS files are linked using a hard-coded URL, such as http://app.com/scripts/dostuff.js.
Is it possible that the browser is reloading the javascript files each time? If so, have I done something wrong?
Try this code:
RewriteEngine On
# skip rewrite rules below it is a valid file or a valid directory
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# write single path
RewriteRule ^([a-z]+)/?$ /index.php?p=$1 [L,QSA]
# write 2 paths
RewriteRule ^([a-z]+)/([a-z]+)/?$ /index.php?p=$1&agency=$2 [L,QSA]
I have a .htaccess file on a website I'm working on which rewrites urls from mydomain.com/sub/folder/ to mydomain.com?index.php?controller=sub&view=folder
Unfortunately the way I've written it means I can't access images, stylesheets and other linked files anymore. Could anyone tell me how best to exclude specific directories / URL requests from the rewrite rule?
Apologies if this is a bit of a newbie question, I'm still wrapping my head around this mod rewrite stuff!
The .htaccess file looks like this:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9-]+)/?$ index.php?Controller=$1
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-_]+)/? index.php?Controller=$1&View=$2
If your images are in mydomain.com/images and you are linking to them using relative links on the page mydomain.com/sub/folder/ the browser is going to try to attempt to access the image via mydomain.com/sub/folder/images/i.gif. But if you change your links to absolute links, the browser will correctly attempt to load mydomain.com/images/i.gif. However, the RewriteRule will change it to: mydomain.com/index/php?Controller=images&View=i.gif. To avoid this you need to add a few RewriteConds:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_]+)\/?$ index.php?Controller=$1
RewriteRule ^([a-zA-Z0-9-_]+)/([a-zA-Z0-9-_]+)\/? index.php?Controller=$1&View=$2
So that when attempting at access an existing file/directory, don't rewrite to index.php.