Got stuck in a htaccess and rewrite - php

This is my first question here, so thank you all in advance and also sorry for my bad english.
I'm trying to apply htaccess and rewrites to one project I'm worknig on, but the problem is that I'd never done this before. I've been reading a lot of post (like http://corz.org/serv/tricks/htaccess2.php or this one http://www.the-art-of-web.com/system/rewrite/1/#.UW7mHqJhXCd) and articles about this, but combined with no previous experience on regular expressions I can't get it done...
I've been with this for 2 days now and I'm running out of time to finnish it, so I was wondering if someone could give me a hand, so here it goes:
WHAT I HAVE:
3 files at my root:
index.php
home.php
404.php
On the database:
On the users table there is a field called user_url
HOW NEED THIS TO WORK:
I want to display every user personal page like this www.example.com/user_url
I'm doing it like this at the moment www.example.com/?id=user_url
But I also need this to be able to go directly to the files in some cases, like with home.php instead of having to make something like ?id=home

Try adding these rules to the htaccess file in your document root, preferably above ones that do other kinds of routing:
Options -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?id=$1 [L,QSA]
As far as this:
But I also need this to be able to go directly to the files in some cases, like with home.php instead of having to make something like ?id=home
I have no idea what you're talking about. If you mean you'd like to go to http://www.example.com/home.php without it getting rewritten to /index.php?id=home.php, then that's what the 2 conditions above do.

Related

Mod_Rewrite Complications

I have recently been doing a lot of redesign on my website, and one of these changes is SEO URL's, and I am doing Mod_Rewrite to do this. The problem is that I am not exactly familiar with reg expressions at all, nor am I exactly the best in the world when it comes to working with the .htaccess file.. Now, the problem I am having is that I have several variables on the same level and the RewriteRules I am using are cancelling each other out. I did find a work around by adding is tag, name, and page into the rewritten url, but that is not what I want. I would like the urls to be structured so it is /$lg/articles/[$tag|$name|$page]/ Again,doing is the way I want causes them to conflict because the document doesn't seem to know to read and process off three at the same time. Any tips and suggestions on how to do this? I would greatly appreciate it. :D
RewriteRule ^([^/]*)/articles/tag/([^/]*)$ /articles.php?lg=$1&tag=$2
RewriteRule ^([^/]*)/articles/name/([a-zA-Z]+)$ /articles.php?lg=$1&name=$2
RewriteRule ^([^/]*)/articles/page/([0-9]+)$ /articles.php?lg=$1&page=$2
What I am wanting it so have the URL's structure like so : /language/articles/Variable Value. Say an integer is put in, I want the url to look like so: /en/articles/1 and it will then act as the page number for the pagination system. Or an article name is put in: /en/articles/random_article_name and that name is then used to all the specific article in a database. The same idea applies for the tags, but pulls all articles with that tag. The idea is to have the URLs short and clean. The problem is that if I do the code like so:
RewriteRule ^([^/]*)/articles/([^/]*)$ /articles.php?lg=$1&tag=$2
RewriteRule ^([^/]*)/articles/([a-zA-Z]+)$ /articles.php?lg=$1&name=$2
RewriteRule ^([^/]*)/articles//([0-9]+)$ /articles.php?lg=$1&page=$2
Only one of the $_GET values will be read, and the rest will be ignored regardless of the variable name or regex expression. I am looking for a way to format my urls like so, if possible. If it isn't, I understand. I just figured it doesn't hurt to ask those who are more experienced.
You can have your rules like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]*)/articles/([0-9]+)/?$ /articles.php?lg=$1&page=$2 [L,QSA]
RewriteRule ^([^/]*)/articles/([a-zA-Z]+)/?$ /articles.php?lg=$1&name=$2 [L,QSA]
I'm not sure what's not working as your rules seem good to me. You can try this one to group the 3 rules :
RewriteRule ^([^/]*)/articles/(page|name|tag)/([0-9]+)$ articles.php?lg=$1&$2=$3

mod_rewrite ignore variable if empty

Ok so i have a project that has product listing by categories,but also listing all products. I created a pagination for it,that works good. I set up mod_rewrite to work with paging on categories,but i cant manage to set it up for all products. The working one is www.domain.com/catalogue/catid/catname/pagenumber, and i also want to make www.domain.com/catalogue/pagenumber, but mod rewrite comes to problem here.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)$ index.php?p=$1&cid=$2&cname=$3&page=$4 [L]
and when i try to call second link,he treats that page number as cid. So i want to set up mod rewrite to read what is needed,and ignore cid and cname in this case.
I'd like to give you an alternative for this one..
Instead of using regexes, which you seem to have a bit of a problem with still.. There's a much easier way.
If you redirect everything to index.php, no exceptions.
Then within index PHP, you can read the current url using $_SERVER['REQUEST_URI'], and it becomes very easy with explode('/', $url);' to do all this processing..
It's a much more natural way to do routing.
There is not an ultra reliable way to do this in mod_rewrite.
The most reliable method is check for this in PHP.
There are two main methods:
One as Evert mentions redirect all to the page in question and decipher the url from there, which is what most do
two, detect the amount of $_GET vars you receive and judge how the page should react to what it gets within the PHP.
What you ask is not very straight forward, nor very fast in regex so I would recommend slapping this clause onto PHP rather than trying to get regex to do it for you.
Why not use two rules, if you're sure you'll never get a link like catalogue/catid/pagenumber ?
RewriteRule ^([^/]*)/([^/]*)$ index.php?p=$1&page=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ index.php?p=$1&cid=$2&cname=$3&page=$4 [L]

php url rewriting and htaccess

I wonder if you can help me with a problem I am having, and give some advice on best practices.
I have a mysql table as follows
teams
columns: id, name, description
data : 1, Aston Villa, text here
I need to do dynamic rewrites to pull out data from the table. The only way I could think to do this was to use _GET as a variable and and look up the name, and change the case to lower case and swap spaces for -. I know there are a few security issues around doing it this way, but its a nice looking url www.mydomain.com/clubs/aston-villa
I have been looking at some frameworks such as yii and they seem to do it php side as apose to in the htaccess file.
Could anyone give me some pointers on the best way to achieve this, I havent managed to find any decent info on the web about best ways to achieve this.
Thanks in advance
Richard
The best solution is to redirect all request to a single file (this is called bootstrapping). The redirect is done in .htaccess like so
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
</IfModule>
Then, in index.php parse the request url, for example, via the $_SERVER values.
Even better, just google PHP bootstrap and get one of the very simple functioning samples/demos
What I use is an .htaccess file with mod_rewrite rule that will redirect all 404 errors to a specific script. From there on, since you have access to the original URL (the pretty one that just triggered a 404), you can parse it and figure out what was intended.

Mod Rewrite with Pagination

I wanna make this rather simple to ask so I can hope for a simple response. I'm somewhat new to mod rewriting (most I've done is a small cms using index.php?page=x and mod rewriting to that name). I have a shopping cart created by foreign people for my company before I started working here with little to no documentation and they are asking me to make the cart search engine friendly. I won't get too dirty with the details, just need to ask a question.
I have, say, results.php?name=friendly-url. I've edited my .htaccess so I can access these pages with a friendly url. It works perfectly.
RewriteRule ^([A-Za-z0-9-]+).html$ results.php?name=1
Now the cart has it written to paginate kinda awkwardly only because the $_GET variable is stupidly named. I'm trying to find out, without having to get really dirty and having to re-name files or re-route directories in the code, to make a simple friendly pagination.
The end result I'm looking for is something like starter-kits-01.html and starter-kits-02.html and so on. This is the mod rewrite I've been trying just to get something to work.
RewriteRule ^[A-Za-z-0-9-]+).html?p=([0-9]+)$ results.php?name=$1&pageNum_rsCWResults=$2
That, I believed, should allow me to render starter-kits.html?p=2. I'm getting no mod rewrite error, but it's messing up my $_GET variables. I can't do, say, /starter-kits/2/ without getting dirty and having to go through this messy code the foreign people made and change 500 lines of directories.
I've spent about 30 minutes on it, and I have 3 other projects going on today, so I'm going to move onto those while I wait for somebody a little more experienced with mod rewriting to give me a helping hand.
Much appreciated.
Just use:
RewriteRule ^([A-Za-z0-9-]+).html$ results.php?name=1 [QSA]
The QSA part tells it to forward any GET parameters on to the rewrite.
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_qsa
The page rewriting you need to do should be done in PHP itself and only require a few lines in mod rewrite. I really recommend you download a copy of Drupal or WordPress to see how they do it. But basically here is how it should work.
You create a URL structure like this:
product-search/cat-toys
product-search/cat-toys/page-1 (should point to the same place as the previous URL)
product-search/cat-toys/page-2 (could also use "product-search/cat-toys/page/2)
You take your site and have everything relay through a central index.php file, mod rewrite takes care of this. You just use the URLs on your site and the PHP will take the params passed and parse it into the URL structure that then takes you to the results.
Essentially the URL path is passed to index.php to one parameter by mod rewrite.
Sample mod rewrite entry (from Drupal, very similar to Wordpress):
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Please check out these systems to get a better idea. WordPress is a much simpler, more straightforward system to go through.

combining multiple htaccess rewrites [duplicate]

I am working on some rewrites and redirects in my htaccess file. My reason for doing this is because I am developing a new design and layout for an existing website therefore I need to redirect to keep hold of the Google rankings etc.
So the old (existing) URL looks like this:
/news/internet-shopper-numbers-continue-to-grow-$21378232.htm
and the new URL will look like this:
/industry-news/small-business-website-design/internet-shopper-numbers-continue-to-grow/800715788
In the old URL internet-shopper-numbers-continue-to-grow is dynamically generated, where as in the new URL small-business-website-design, internet-shopper-numbers-continue-to-grow, and 800715788 are all dynamically generated.
So my issue is how do I get from 1 URL to another if only 1 of the dynamically generated variables is available.
I was thinking perhaps it would be a good idea to do a PHP redirect instead where I can get the data from the URL process through database to get all data needed to generate the new URL, but I'm not sure if this is good practice, this would also mean having to match the old URL file directory which is not the current case.
I'm guessing I start with something like this:
Options +FollowSymlinks
RewriteEngine On
RewriteCond /news/%{QUERY_STRING}(.)(\\d+)(.)(htm)$
RewriteRule ^industry-news/(.+)/%1/(\d{9})
I know this is very basic and kinda wrong but I'm working on this for the first time and I'm stumped.
-- extra question --
am I able to do something like this?
RewriteCond /new/%{QUERY_STRING}(.)(\\d+).htm$
RewriteRule ^industry-new/article/article.php?direct=%1
I dont know how to go about testing these really, but would that work?
Yes, it's definitely a good idea to do all redirection stuff in php. Just have a simple "catch-all" .htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php
then transform urls as you want in php and send a location header when necessary. It might be a good idea to add 301 status code for "old" urls.

Categories