URL Rewrite best practice & how to set it up with htaccess - .htaccess

I have a small personal project I'm working on, and before I get too deep into it, I'd like to get some opinions here on the best practices regarding the set up and URL scheme... and how I might set up the .htaccess rewrites for the following. My questions are:
Is the below set up the best way to manage user information in a small user profile setup?
I have worked out how to get the subdomain working for the rewrite username.domain.com with wildcard subdomains and httpd.conf file set up... but I am stuck on the rest of the scheme seen below. Basically, a user profile will always result in username.domain.com and then appended with the various pages within their account (photos, videos, notes etc). How would I set up the .htaccess rewrites to accommodate this? I really appreciate any advice here. I have done a ton of research here on stackoverflow, and on other sites, but I can't find a decent explanation to achieve this.
Thanks for any help.
www.domain.com/profile.php?u=username --> username.domain.com
www.domain.com/photos.php?u=username --> username.domain.com/photos
www.domain.com/photos.php?u=username&a=album --> username.domain.com/photos/album
www.domain.com/photos.php?u=username&a=album1&p=photoid --> username.domain.com/photos/album1/photoid
www.domain.com/settings.php?u=username --> username.domain.com/settings
etc

Your proposed setup looks fine to me. Here are some rules for .htaccess (make sure you have mod_rewrite enabled and AllowOverride All set in your httpd.conf):
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} (.*)\.domain\.com
RewriteRule ^$ http://www.domain.com/profile.php?u=%1 [L,QSA]
RewriteRule ^photos/?$ http://www.domain.com/photos.php?u=%1
RewriteRule ^photos/([^/]+)/?$ http://www.domain.com/photos.php?u=%1&a=$1
RewriteRule ^photos/([^/]+)/([^/]+)/?$ http://www.domain.com/photos.php?u=%1&a=$1&p=$2
RewriteRule ^settings/?$ http://www.domain.com/settings.php?u=%1

Wich server do you use? And first of all use rewrites in the config file of your server, not htaccess, htaccess slows the server down.
Edit: Iam not shure about the speed, but as far as i remember htaccess slows down apache, i dont know about how much. Iam sry you have to google that :)

Related

mod_rewrite and dynamic pages

I tried following a tutorial on the internet about mod_rewrite but it wasn't really for me. I created a .htaccess file that has the following code for now:
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
From what I understand this is the basic setup of .htaccess to rewrite urls, followed by the instructions... what and how to change. I tried different exampled but it didn't worked for me. I have a dynamic page with the url localhost/alpha/oferta.php?id=52042156c65d4, where id="..." is the unique id of that offer. I want to change it to localhost/alpha/oferta/id=".."
Can you please show me an example of how can I achieve that? Also if you know any helpful tutorials let me know. Let me know before downrating so I can edit my question. Thanks!
So you want this kind or URL : localhost/alpha/oferta/id=123abc to be redirected to localhost/alpha/oferta.php?id=123abc.
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^alpha/oferta/id=([A-Za-z0-9]+)$ alpha/oferta.php?id=$1 [L]
Remember a few things :
this won't magically change "old" URLs into "new" ones. You must use rewritten ("new") URLs everywhere. Then your htaccess will change this readable URL into a technical one, which can be used by your code.
this redirection is transparent. If you want the URL to change into the browser bar, use [L,R=301] instead of [L].
this will only accept letters (case insensitive) and numbers for your id.
you can find a good cheat sheet about mod_rewrite here.

The requested URL /public_html/ was not found on this server

I'm working on a local MAMP website. I use a micro MVC framework to use friendly urls
so I don't need to call index.php (which is inside of public_html directory) in the urls.
To achieve that, I have the following htaccess:
RewriteEngine on
RewriteCond %{REQUEST_URI} !public_html/
RewriteRule (.*) /public_html/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
Then I have the following line in /etc/hosts
127.0.0.1 mywebsite
Also, I have the following in httpd.conf
<VirtualHost *>
DocumentRoot "/Applications/MAMP/htdocs/mywebsite"
ServerName mywebsite
</VirtualHost>
So if I simply call http://mywebsite from the browser, the whole thing works smoothly.
So what's the problem?
The problem appears if I try to reach the same page from another machine in my LAN.
So if I write http://192.168.1.15/mywebsite the answer is:
Not Found
The requested URL /public_html/ was not found on this server.
I get the same message if I call http://localhost/mywebsite from my own machine.
I have the feeling that is something related to .htaccess, but I've been trying a
lot of different ideas I've found in the web, and nothing works.
I'd like to fix this, because I need other people to check the website from their machines.
If you have any clue please help. Thanks a lot.
Edit: I can't solve this, so as a temporary fix I've created a free account at AppFog for my team to be able to access the page until we go to production.
After all these years, I thought that computing would be easier... it's getting harder, actually. The htaccess file is a huge mistery to me! Thanks anyway :)
By the way... as I told you, I've found a way to fix the problem. The funny thing is I have to use a different .htaccess file. I thought you could be interested, provided that it seems you like computing stuff :)
This is the .htaccess that works in my LAN debian server:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/(www/.*)$
RewriteRule ^(.*)$ index.php [QSA,L]
As you can see, it's different from the one I showed you at my first post. My conclusion? Well, even when I always try to keep things simple, life shows me one and another time that everything can be more complicated. Look at the .htaccess file. It's so funny... I've learnt to work with it by using different combinations of code.
Isn't it crazy? Yes it is. Please let's do understandable software. Thank you!
Ok, I've been trying a lot of different things and all of them fail. The only solution I've found is to install the web site in a separated LAN debian server. Everything works ok in that way, but it's not possible to make it work from my machine with MAMP.
Why not? Well, I don't know. After a lot of years in computing, I've learnt to say "I donĀ“t know", you know what I mean.
I've been working for a few days with an AppFog account for free but, you know, that thing cannot work when you put a database and the whole thing there. Obvious.
So at the end the only solution I've found has been to put everything at that local LAN debian server.
Thank you anyway :)

.htaccess rewrite url with parameter

I want to change my url with only the parameter. As I am passing only one parameter, so I want the sub url should be changed with my parameter name. I want change the url as the following type-
From:
http://www.xyz.com/cat.php?slag=season
to
http://www.xyz.com/season
Can anyone help me to do it. I don't know how to do it. Thanks
Options +FollowSymLinks
RewriteEngine On
RewriteBase /path/to/your/directory
RewriteRule ^(.*)cat/(.*)$ cat\.php?slag=$2&%{QUERY_STRING} [L]
put the above in your .htaccess file.. and that would do the magic..
Note :
RewriteBase /path/to/your/directory
(use this line only if your application is in any of subfolder the folder)
I suggest you obtain a copy of the .htaccess that WordPress uses. It's a quite powerful starting point that allows you to handle routing internally from within your Application - which you might feel more comfortable with, as it seems to me.

removing file extension with htaccess failing

i'm using an htaccess script trying to remove the .php testing the .htaccess on a testing server it runs fine, but on the live server that is a different host it trys rewriting the file based on the absolute path and the rewrite fails
here is the htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
this is taking a url like this www.example.com/services
and trying to point it to /n/c/example.com/public/service.php
I know the {REQUEST_FILENAME} is suppose to be pulling the full local system path, but i don't understand why it's not finding the file. i know very little about htaccess and mod_rewriting so i'm not really sure what I should try to make it base everything off of just the url path, or if there is a better solution. I'm really open to suggestions.
Any help would be greatly appreciated.
Thanks
Use RewriteRule .* %{REQUEST_URI}.php [L]
It is hard to tell why your rule did not worked for you by having so little info about your Apache setup and any other rewrite rules that you may have.
Quite possible that the [L] flag did the trick for you -- you may have other rewrite rules that were rewriting this URL further, producing incorrect result in the end. I don't think that %{REQUEST_URI} did such a big job on its own, unless you have some symbolic links / aliases or even some transparent proxy in use which could make a difference.
Keep in mind, that the rules you have shown in your question cannot generate this sort of URL to be visible in browser's address bar (example.com//service.php/) -- it has to be a redirect (3xx code) involved .. which suggests that you have other rules somewhere.
Most likely it is a combination of your Apache specific settings & combined rewrite rules logic (where the L flag can make a big difference depending on those other rules).
The only way to give more precise answer will be enabling rewrite debugging and analyzing how rewrite was executed and what was involved.
Have you enabled mod_rewrite on the other server? AddModule mod_rewrite, I think.
Also - more likely - have you enabled .htaccess? You would need to have
AllowOverride All
or
AllowOverride FileInfo
for that.
These directives will need to go in the apache config files (usually /etc/httpd/conf/httpd.conf or one of the files in /etc/httpd/conf.d), and you will need to restart apache to get them to take effect.

I need some help with doing a RewriteBase for .htaccess

I recently got an arcade script through a membership site that I belong to. The owner of the site offered free install of the script. I registered a domain name and got a hosting account with godaddy.com The script was installed, but I am having a slight problem. Everything else seems to be working fine, except for clicking on the links on the left hand menu that says "Categories".
Here is my site: http://www.freeonlinegamerooms.com
I called GoDaddy customer service and was advised that I needed to add a Rewrite Base to my .htaccess file . They sent me a link to the apache site, but it hasn't helped me at all, as I am a non-technical person. It's all Greek to me. The guy who installed my script has tried to get it fixed, to no avail. He said he isn't a progammer either. Here is the text of my .htaccess file, as it presently exists:
Options +FollowSymLinks
RewriteEngine on
RewriteRule "^game/([0-9]+)/$" file.php?id=$1 [Last]
RewriteRule "^category/([0-9]+)/([0-9]+)/$" category.php?id=$1&page=$2 [Last]
Can anyone help me with this, please? All I want is a site that works properly. I am sure this something very easy for a programmer to figure out, I'm just at a loss myself because I'm not a programmer.
Thanks in advance for any help/advice
Doug
I think you need to put a slash in the rewrite rule after the ^.
Options +FollowSymLinks
RewriteEngine on
RewriteRule "^/game/([0-9]+)/$" file.php?id=$1 [Last]
RewriteRule "^/category/([0-9]+)/([0-9]+)/$" category.php?id=$1&page=$2 [Last]
edit: this is wrong, see my follow-up comment in this answer.

Resources