Creating user/search engine friendly URLs - mod-rewrite

I want to create a url like www.facebook.com/username just like Facebook does it. Can we use mod_rewrite to do it. Username is name of the user in a table. It is not a sub directory. Please advise.

Sure, mod_rewrite can do that. Here is a tutorial on it.

Yes you can do this but you might have a couple of initial hurdles to get it going correctly.
The first is that you will have to use a regular expression to match it. If you don't know regex then this can be confusing at first.
The second is that you will need to take into account that of you are going to rewrite the top path on the domain you will have to have some mechanism for only rewriting if the file doesn't exist.
I guess if mod_rewrite supports testing if the url points at a real file that will be easy. If not you might have to use a blacklist of words that it wont rewrite as you will need to have some reserved words.
This would include at the least the folder that contains your images, css, js, etc and the index.php your site runs off, plus any other php files you have kicking around.
I would like to be more help but I am a .net guy and I usually help out in asp.net url rewriting issues with libraries such as UrlRewriter.net which have different configurations than mod_rewrite.
To match the username I would use a regex like this:
^/(\w*)/?$
this would then put the bit in the brackets into a variable you can use in the rewrite like
/index.php?profileName={0}
The regex I provided means:
^ nothing before this
/ forward slash
(\w*) any number of letters or numbers
/? optional forward slash
$ nothing after this

Related

URL rewriting all pages into one page?

I'm trying to get all URLs except for a few to rewrite to _main.php?url=/url/path/here (the exceptions being a System, Frameworks, _Assets, or Administrator folder). I have a RegEx that does this matching for me: (^|\n)(?!/?(?:_Assets|Administrator|Frameworks|System)).+ but I am not really understanding how to do this properly in IIS8. What it currently does is give me the correct data when I load the page, but it doesn't rewrite the URL, as seen in this image:
My rule looks like this (with no conditions or variables):
I was also trying to follow this guide, but it seemed like the exact opposite of what I wanted to achieve, and I don't know how to do the reverse.
I managed to solve this by changing the regexp to be (^|\n)(?!_main.php\\?)(?!/?(?:_Assets|Administrator|Frameworks|System)).+ (ignoring _main.php) and then changing the rule to be a redirect instead of a rewrite, which achieved the desired effect.

mod_rewrite to string special characters and shorten URL simultaneously

Gang,
Long time sysadmin but first time poster to this excellent site, so, please be gentle.
I am not strong at REGEX yet and trying to do two things at once on our internally hosted "mediawiki" site.
We are running an otherwise pretty plain jane LAMP stack (centOS 5.x, Apache 2.x, PHP 5.x). We are root. We are using /etc/httpd/conf.d/wiki.conf and not using .htaccess. The physical path is /var/www/html/wiki/
I have partially successful results with some combination of the below, but I am not good enough to get it all the way there. I know that there are some mod_write studs on this site that I am hoping to avail.
I am following this recipe https://www.mediawiki.org/wiki/Manual:Short_URL/Apache so as to shorten URL's from www.example.com/wiki/index.php?=title=Garden_Store to www.example.com/wiki/Garden_Store
still allow the use of www.example.com/wiki/index.php?=title=Garden_Store should the user should choose to type out that syntax of URL. (I believe that is possible with mediawiki to use both style URL's at the same time. If it is impossible, then I will be forced to skip the short URL and use the style with the variable in it.)
Last, string special characters from the URL in the example like www.example.com/wiki/index.php?=title=Garden,_Store! ought to be this www.example.com/wiki/index.php?=title=Garden_Store .
Another example of that might be www.example.com/wiki/index.php?=title=Garden_Store,_Inc. ought to be www.example.com/wiki/index.php?=title=Garden_Store_Inc
One last example, us to make sure that I am communicating well, would be getting this "/title=Garden%20Store,%20Inc" but wanting this "/index.php?title=Garden%20Store%20Inc" as I know that the spaces are replaced with underscores inside of mediawiki.
Thanks so much for walking a newbie the last bit to the finish line on this one.
Cheers.
Jason
Something like the following rules should do what you need:
RewriteRule ^(.*)\ (.*)$ $1\_$2 [L]
RewriteRule ^(.*)[^a-zA-Z0-9\/\._](.*)$ $1$2 [L]
First rule does replace space with underscore and the other line strips all chars you don't want to stay in the resulting URL. Note, your will probably need to add some more, if you want.

Zeus Rewrite Rules

I have a website that renders the URL:
/work.php?cat=identity
Normally I would research how to use mod_rewrite but unfortunately my hosting (Namesco) uses Zeus and not Apache, which is strange. How would I use Zeus' rewrite rules to convert to:
/work/identity
This is a much cleaner, nicer SEO friendly version. On top of this, I still need the $_GET variable to be active because it requests information about the variable cat from the database.
I've never rewritten URLs before so I've no idea where to begin. I've attempted the change with this rewrite.script file which is saved within my web folder
match URL into $ with ^/work.php?cat=/(.*)
if matched set URL= /work/$
Unfortunately it doesn't work. Can anyone help or perhaps offer an alternative?
had a quick play with this, and I believe I have proven to myself that the Request Rewriting is not able to manipulate the query element of the URL.
There is a potential solution, but it gets even more ugly!
You could use the "Perl Extensions" of ZWS to achieve this. Essentially you pass the request to the Perl engine within ZWS run a script against it, then pass the result back to the ZWS.
I am afraid this is a bit beyond my capabilities however! I am a "Zeus Traffic Manager" sort of chap...
Nick
Zeus Rewrite Rules are able to access the query part of a URL string. The key thing your missing it looks like is the 1 following the $ on the output URL and the slash should be removed:
match URL into $ with ^/work.php?cat=/(.*)
if matched set URL= /work/$
should be
match URL into $ with ^/work.php?cat=(.*)
if matched set URL= /work/$1
I am wondering if the rewrite rules are available for the query portion of the URI? The docs do seem to only speak about the path element.
http://support.zeus.com/zws/docs/2005/12/16/zeus_web_server_4_3_documentation
page 141 seems to be the start of it...
I will attempt to fire up a ZWS VM and test this myself.
Nick

mod_rewrite: how to strip url of query string yet retain it's values

I'd like to strip a URL of it's query string using mod_rewrite but retain the values of the querystring, for example, id like to change:
http://new.app/index.php?lorem=1&ipsum=2
to a nice clean:
http://new.app/
but retain the values of lorem and ipsum, so inside index.php:
$_GET["lorem"]
would still return 1 etc.
This is my first dabble with mod_rewrite so any help is greatly appreciated, and if you could explain exactly how your solution works, I can learn a little for next time too!
Thanks!
As Roland mentioned, you don't seem to understand the way rewriting works. It's typically done using Apache mod_rewrite in .htaccess, which silently rewrites the pretty URLs to the php script as /index.php?lorem=1&ipsum=2
Even Joomla uses .htaccess, except it has a single rewrite rule that passes EVERYTHING to a PHP script which does the actual rewriting in PHP.
What you are not understanding is that something still needs to exist in the "pretty" version for the php script to pull the value of $_GET["lorem"]
So it would be like http://new.app/lorem/ or http://new.app/section/lorem which would then (using mod_rewrite in .htaccess) rewrite TO the php script.
I don't understand exactly what you want. Your first URL is the external form, which the users see and can type into their browsers.
The second form has almost all information stripped, so when you send that to a server, how is the server supposed to know that lorem=1&ipsum=2?
If your question is really
How do I make the URLs in the browser look nice, even if the user is somewhere deep in the website clicking on URLs that carry lots of information?
then there are two solutions:
You can pass the information in small bits to the server and save them all in a session. I don't like that because then the user cannot take the URL, show it to a friend and have him see the same page.
You can have your entire web site in an HTML <frameset> containing only one <frame>. That way, the URL of the top-level window will not change, only the inner URL (which is not displayed by the browser) will.

Rewrite URL to HTML file causing rewrite to .html/

I'm currently working on an overhaul of my blog site, and have found a way to convert all my current pages into static html pages. They are currently using friendly url's which remap to a central index.php page with GET parameters attached on.
The change I am trying to make is have those same friendly URL's map to their html counterparts. I am currently using this rule:
RewriteRule ^archives?/([^/]+)/([^/.]+)/?$ archives/$1/$2.html
The error log is reporting that it cant find blah.html/ which means it's looking for the .html directory, instead of the .html file. So a better example:
/archives/2009/original-name
should be getting mapped to
/archives/2009/original-name.html
but is really getting mapped to
/archives/2009/original-name.html/
What am I missing here?
don't you need to use it the other way around? I didn't test the code but it should be something like this:
RewriteRule ^archives/(.*)/(.*).html archives/$1/$2
I can't see anything obviously wrong with your regex.
At a guess I'd say you might have a rule somewhere following this, which is redirecting anything without a trailing slash to its equivalent with the slash (a common thing to do to avoid duplicate content issues).
You didn't escape your period in the 2nd statement. Try this.
RewriteRule ^archives?/([^/]+)/([^/\.]+)/?$ archives/$1/$2.html

Resources