How to hide page extension on web addresses - url-rewriting

I've noticed that on several web pages e.g. StackOverflow instead of stackoverflow.com/questions/ask.php they have stackoverflow.com/questions/ask.
How is this done?

If I understand your question right, you just want to have file-names|tags|titles|categories after the last slash to route semantically to the according file or a hidden path;
If so, you will gain this dependent on the possibilities you have:
A) Do you have access to httpd.conf on your Server?
Look out for the DirectoryIndex;
OR
B) If you are not able to edit httpd.conf, do you have access to .htaccess on your server?
Look out if DirectoryIndex is already set anywhere, in most cases it won't; so you want to create your own new entry;
TODO:
Now define the DirectoryIndex by letting this reserved word be followed by the filenames you want to have
been routed to another filename or path/filename
GODO:
DirectoryIndex
[SPACE]
file-name.ending OR file-name OR tag OR title-with-some-words-bond-together
[SPACE]
repeat step 3 and 4 until you have a set of catchers to be caught ...
at least: add the target of your set by spending a path
DOJO:
RELOAD your server
OR
RESTART if necessary
EXAM:
DirectoryIndex index.html index.htm index.pl index.asp index.jsp index /routed/to-path_of/index.php
DirectoryIndex ask ask.php
MOJO:
This is a quant of what you can achieve; Look out for pretty-printing URL's, apache mod-rewrite, DirectoryIndex, URI URL hiding, domain forwarding and similar keys you can google for.
In most cases it would have been realized by well-known BLOG/CMS-SoftWare like WordPress, JOOMLA, DruPal, etc. via mod-rewrite, as this is the controlled and preset way if necessary rights are given.
Personally i would recommend to use the simple format of setting DirectoryIndex via .htaccess, as i explained before, because if you realize your own magic-words you may want to write a script that checks for hash-changes, sniffs history and responds accordingly to server-errors.

If you program it in DJango, NodeJS, or other WebApp language, they actually parse the URL and do not give you files. Instead it is known as views and map into a certain piece of code.
I suggest your learn NodeJS.

It is done through a .htaccess file...

Related

htaccess problems ("/file/" serves file.php)

I was starting to implement mod_rewrite rules on my site when I came across some weird behaviour. I removed my htaccess file for this test, to take it out of the equation.
My local dev site is at http://dev.mydomain.com and is a virtual host.
If I go to, eg "http://dev.mydomain.com/blog/", that folder doesn't exist, but apache finds a matching php file "blog.php" and instead displays that.
This only happens when there is a matching php file - when there isn't, eg "http://dev.mydomain.com/barfblurg/" I just get a 404.
It's like there are some extra mod_rewrites going on above where the site htaccess would be - that when /file/ couldn't be resolved, it searches for other matching files and instead serves this - but there are no other htaccess files that would have an effect, so this must presumably be a config thing? I can't see anything in the apache.conf or php.ini that would cause this behaviour.
(This also doesn't happen on my live host elsewhere, so it's definitely a config thing.)
Anyone point me to where to turn that behaviour off, because it's interfering with the url rewrites I want to do?
(Apache2, OSX, 10.10.5)
This behavior is due to enabling of option MultiViews.
Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.
To turn this off use:
Options -MultiViews
at top of your .htaccess or in Apache config/vhost file.

Apache2 mixing mod_vhosts_alias and mod_rewrite

The Apache2 docs recommend mod_rewrite as a last resort for specifying which directory to send a given host request to. They said use mod_vhosts_alias. I set that up and its working without problem. However, I have a specific case that entails some mod_rewriting.
I have a domain like mydomain.com and a large number of subnames like sub.mydomain.com and sub2.mydomain.com. These subdomains all map to corresponding directories. But the subdomains will also have full top-level domains that map to respective directories. For instance:
sub.mydomain.com will map to the same directory as awesomeproducts.com sub2.mydomain.com will map to the same directory as widgets.com
What would be the best way to make sure both these methods of accessing each site will work without conflict?
Since the docs only say mod_rewrite isn't as "graceful" as mod_vhosts_alias, I didn't know if I ought to use mod_rewrite completely by itself for my situation or if I should be trying to mix the two approaches somehow.
Is mixing them the way to go or will that create problems?
The behaviors of RewriteRule and RewriteCond change when using mod_vhost_alias. Its been a few days since I tinkered with it so I'm sketchy on it, but watch out for the way RewriteRule interprets the portion of the URL to be rewritten. It may change when using mod_vhost_alias... and RewriteCond behavior might change too.
To accomplish the question I was asking about, I ended up using symbolic links for the actual domains. I setup a bunch of subdomains with associated folders so that sub1.stddomain.com would go to the folder sub1.stdomain.com. Then I added a symbolic link that maps domain1.com to the sub1.stddomain.com folder. That way anyone who visits domain1.com is shown the site in the sub1.stddomain.com folder.
IMPORTANT: After all that work and testing, I ended up going back to to a collection of virtualhost files WITHOUT mod_vhost_alias. Turns out that there is a known problem with mod_vhost_alias. DocumentRoot isn't correctly set when using it. This breaks tons of scripts. It was patched in February 2012 with new special variables and the programming team at Apache dropped the ball. They never wrote any documentation explaining how to use the patch or new variables.
I reopened the issue and stated that its still a bug since you can't write code and not tell anyone how to use it and then call it fixed. Unfortunately the issue had already been ignored for YEARS and it will probably continue to get ignored even though they supposedly wrote code for it.
RECOMMENDED LESSON FROM ALL THIS: Don't use mod_vhost_alias. Write shell scripts to manage your vhost files using one more template files.

Multiple index files in different folders?

I just figured out that if I have multiple folders in my website such as logout and login I can then have a log out/in page within the folders called index.php
Now when someone goes to project.com/login/ it will load up the index.php file in that folder and the url will look nice.
Is there any problem in doing this?
As Candy Pointed out it is better to do this with url rewrite rather heeps of folders and index files. I went and looked it up and you can do a lot of cool things.
for the basics:
http://www.addedbytes.com/for-beginners/url-rewriting-for-beginners/
http://www.yourhtmlsource.com/sitemanagement/urlrewriting.html
Instead of creating many folders which would make the site confusing (for the developer) and having numerous index.php files which would also get confusing if you were editing more than one at a time, you can use the apache mod_rewrite module which lets you transparently (the url in the address bar stays the same) redirect one URL to another. You could use this to clean up messy url's and have /login/ instead of /login/login.php or /user/1335591/ or even /user/'username'/ rather than an ugly url which is hard to remember like the following: /user/user.php?user=1335591
to start first put RewriteEngine on in your .htaccess file to turn it on
redirects are then written in the .htaccess file in sequence
Below is an example:
RewriteRule ^products/([0-9][0-9])$ /products/$1/ [R]
RewriteRule ^products/([0-9][0-9])/$ /productinfo.php?prodID=$1
If the user types in products/12 the first rule will append a trailing slash to it. The second rule will transparently redirect this URL to productinfo.php?prodID=12

Magento installation Problem in plesk

I have installed magento from Plesk,The home is well,with layout buy when I click any link it show "Not Found" message.
I can not figure out the problem
Thanks in advance.
It looks like you have a problem with Apache mod_rewrite.
Anyway, you can check is it true. Let's just say that you where going to open http://www.example.com/electronics and got 404. Ok, no panic. Try to edit URL to be like this: http://www.example.com/index.php/electronics
If it works, than definitely you have a troubles with one of the following:
Apache mod_rewrite is not enabled. If you can open a page with phpinfo(), you may try to search "mod_rewrite" on that page. No results found -- no mod_rewrite... Call your hosting support.
You need to set RewriteBase directive in your .htaccess file (it may be hidden for web based filemanager). By default the line with RewriteBase is commented, and you can uncomment it and leave it's default value "/" if your Magento is uploaded to document root. If your Magento is in some sub-directory under document root, you need to set RewriteBase value lie this "/my_folder_with_magento".

mod_rewrite document root change or symlink

I am trying to find the best way of displaying content that resides under a different server location.
So I have a domain where have the main site content is located at:
/home/user/my_site/www/
and accessed at:
www.example.com
I have another site (a blog) located at:
/home/user/the_blog/www/
I wish to get the blog content to appear at:
www.example.com/news
I was planning on using an .htaccess file at my_site to set the rules for the path:
/news
However the content for the blog resides outside the .htaccess document root, so although U can set a rule it won't be able to access this content.
Is it possible to change the document root somewhere higher up the chain?
Or is it possible to just create a symlink for the /news folder? Is this even advisable?
Thanks in advance
Tom
You could set an alias to that location:
Alias /news /home/user/the_blog/www
But that can only be set in the server or virtual host configuration context and not in a .htaccess file.
Since both directories aren't in your DocumentRoot, I don't see how mod_rewrite can work here. And I don't think anyone would recommend symlinking. The way I see it, there are only two ways out of this: either change your DOcumentRoot or move the latter directory into the current DocumentRoot.

Resources