URL rewrite using .htaccess. Which rule should I put first? - php

I want to rewrite:
www.domain.com/entry/1 or www.domain.com/entry/1/title to www.domain.com/entry.php?id=1
www.domain.com/entry/1/title/page/2 to www.domain.com/entry.php?id=1&page=2
www.domain.com/entry/1/title/comment to www.domain.com/entry.php?id=1&a=comment
I have the following in the .htaccess files
RewriteRule ^entry/(([0-9]+))(?:/([^/]*))?/?$ ./entry.php?id=$1
RewriteRule ^entry/([0-9]+)/([^/]+)/page/([0-9]+)$ ./entry.php?id=$1&page=$3
RewriteRule ^entry/([0-9]+)/([^/]+)/([^/]+)$ ./entry.php?id=$1&a=$3
It's working. I also tried reverse the orders to :
RewriteRule ^entry/(([0-9]+))(?:/([^/]*))?/?$ ./entry.php?id=$1
RewriteRule ^entry/([0-9]+)/([^/]+)/([^/]+)$ ./entry.php?id=$1&a=$3
RewriteRule ^entry/([0-9]+)/([^/]+)/page/([0-9]+)$ ./entry.php?id=$1&page=$3
It's also working. How do the lines on the .htaccess file are executed? Which of the versions above is the more correct one? Thanks!

It shouldn't matter. But be sure to put an [L] at the end of each RewriteRule line so that Apache stops after successfully applying a rule and doesn't loop endlessly.
This is also a very useful tool for testing RewriteRules and whether they work correctly.

Related

htaccess 3 different urls in one php file

I have 3 step in one php file:
This is my htacess now:
RewriteRule ^igra/(.*)$ "/index.php?page=igra&id=$1"
RewriteRule ^igra/(.*)/sezona/(.*)$ "/index.php?page=igra&id=$1&season=$2"
RewriteRule ^igra/(.*)/sezona/(.*)/liga/(.*)$ "/index.php?page=igra&id=$1&season=$2&league=$3"
When i go in browser something like index.php?page=igra&id=$1 or index.php?page=igra&id=$1&season=$2 or index.php?page=igra&id=$1&season=$2&league=$3 sure with real values it works fine, but when i try to access with this pretty links it always show me the first rewrite rule..
I hope u understand me what i need here, best regards..
Your first rule is capturing everything, so the subsequent rules never get executed. Just switch them around:
RewriteRule ^igra/(.*)/sezona/(.*)/liga/(.*)$ /index.php?page=igra&id=$1&season=$2&league=$3 [L]
RewriteRule ^igra/(.*)/sezona/(.*)$ /index.php?page=igra&id=$1&season=$2 [L]
RewriteRule ^igra/(.*)$ /index.php?page=igra&id=$1 [L]
Notice also the addition of the L flag.

$_GET & .htaccess | PHP ignorer values

I got this problem, where I try to make an Image Archive. Most of it works, but I have now come to prettifying URLs. I have worked with this before, but somehow, it don't work now. If I add more then one Rewrite URL to the same file (index), it ignores the values added with it.
What I got:
Header add "disablevcache" "true"
RewriteEngine On
RewriteBase /folder/
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteRule ^archive/([^/]+)?$ archive/index.php?cat=$1 [E=ORIG_URI:/$1]
RewriteRule ^archive/([^/]+)/([^/]+)?$ archive/index.php?cat=$1&sub=$2 [E=ORIG_URI:/$1]
RewriteRule ^archive/([^/]+)/([^/]+)/([^/]+)?$ archive/index.php?cat=$1&sub=$2&id=$3 [E=ORIG_URI:/$1]
RewriteRule ^archive/([^/]+)/([^/]+)/([^/]+)?/([^/]+)$ archive/index.php?cat=$1&sub=$2&id=$3 [E=ORIG_URI:/$1]
The following should allow me to access these URLs:
domain.com/archive/category
domain.com/archive/category/subcategory/
domain.com/archive/category/subcategory/id
domain.com/archive/category/subcategory/id/item
domain.com/archive will be made default, since I use an index.php file, in a folder. And item will just be for SEO and has nothing to do with PHP. (The last rewriteRule)
This works. I can access the same page, from all the different URLs. (archive/index.php). But when I try to use PHP to get the value from the provided URLs, it won't work. I can't even check and see if there are values.
No matter what I do, it just echo out Category & Sub Category on every URL. Also if I go to the default URL, domain.com/archive.
if($_GET['cat']){
echo 'Category';
if($_GET['sub']){
echo 'Sub Category';
}
}
Anyone who can see what I am doing wrong or got a suggestion to why it ignore my values? Or maybe know another way to do this, so I end up with the same result?
Apache rewrites in a loop. You need to stop it for archive/index.php since that always matches the first RewriteRule:
RewriteRule ^archive/index\.php$ - [L]
RewriteRule ^archive/([^/]+)?$ archive/index.php?cat=$1 [E=ORIG_URI:/$1]
RewriteRule ^archive/([^/]+)/([^/]+)?$ archive/index.php?cat=$1&sub=$2 [E=ORIG_URI:/$1]
RewriteRule ^archive/([^/]+)/([^/]+)/([^/]+)?$ archive/index.php?cat=$1&sub=$2&id=$3 [E=ORIG_URI:/$1]
RewriteRule ^archive/([^/]+)/([^/]+)/([^/]+)?/([^/]+)$ archive/index.php?cat=$1&sub=$2&id=$3 [E=ORIG_URI:/$1]
You can also remove [E=ORIG_URI:/$1] if you don't use it.

Trouble mapping multiple subfolders to destinations in htaccess

We've switched servers and for whatever reason, our htaccess file isn't behaving the same way as it was on the other. I'm going to be the first to admit that I'm not an htaccess superuser, and I have no doubt the answer's probably looking me in the face, but literally an entire Saturday of searches hasn't fixed this. I have this filesystem:
When the domain is /category/subcategory/product/
It should rewrite to /category/product-details.php?p=product&s=subcategory
When the domain is /category/subcategory/
It should rewrite to /category-product-list.php?slug=subcategory
This seems simple enough, here's the same code we had been using for years. Note, we've commented out the first two RewriteRules regarding slashes and there was no change in behavior.
RewriteEngine On
RewriteBase /
# if folder does not end with a slash redirect to with slash
RewriteRule ^([-a-zA-Z0-9]+)$ /$1/ [L,NC,R=301]
#if it does not end with a slash e.g. rock-jewelry/some-piece, add the slash
RewriteRule ^([-a-zA-Z0-9]+/[-a-zA-Z0-9]+)$ /$1/ [L,NC,R=301]
RewriteRule ^category/([^/]+)/([^/]+)/?$ category/product-details.php?p=$2s=$1 [L,QSA]
RewriteRule ^category/([^/]+)/?$ category-product-list.php?slug=$1 [L,QSA]
When the domain is /category/subcategory/product/
It rewrites to /category-product-list.php?slug=product-details.php&p=product&s=subcategory
When the domain is /category/subcategory/
It correctly rewrites to /category-product-list.php?slug=subcategory
/category/ is an actual folder, and the only thing in it is product-details.php, there is no index.php file, the htaccess is supposed to rewrite to category-product-list if they're trying to access the index.
If we remove the category-product-list.php rule, the product-details.php rule DOES work. But isn't the L directive supposed to stop at the first rule? Why is the second still running? And how can I write a better way to accomplish this goal? Thank you very much, I'm pretty beat down on this problem at this point.
I have the same problem as you to understand the why of this loop... But it's like that.
You can add that before the last RewriteRule:
RewriteCond %{REQUEST_FILENAME} !-f
Or if you don't use dot in subcategory, you can use final RewriteRule:
RewriteRule ^category/([^./]+)/?$ category-product-list.php?slug=$1 [L,QSA]

.htaccess - How to make an exception without redirection

So, I've a big problem with .htaccess. All addresses are redirected to app/webroot. I'd like to make an exception without redirection for my file: cron-5min.php.
My part .htaccess:
RewriteRule ^cron-5min.php$ cron-5min.php
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
Unfortunately, when I type address.com/cron-5min.php it redirects me to file for path /app/webroot/cron-5min.php
I tried to add instruction RewriteRule ^cron-5min.php$ cron-5min.php in a variety of places in .htaccess, but nothing helped.
Thanks in advance!
Your rewrite rule does not terminate the chain. Note your other two rewrite rules are appended with [L] (Last). This indicates that no more rules should be processed when it matches.
You can also use the [END] flag, which is the same as [L], but no more rules will be processed from subsequent .htaccess files in this requrest.
More info on both these flags and using flags are in the documentation:
http://httpd.apache.org/docs/current/rewrite/flags.html

Apaches mod_rewrite issues

I'm having issues with apaches mod_rewrite. I'm wanting to make clean urls with my php application but it doesn't seem to give the results i'm expecting.
I'm using this code in my .htaccess file:
RewriteEngine on
RewriteRule ^project/([0-9]{4})/([0-9]{2})$ /project/index.php?q=$1&r=$2 [L]
RewriteRule ^project/([0-9]{4})$ /project/index.php?q=$1 [L]
To make it so when I view, http://localhost/user/project/system, it would be the equivelant of viewing http://localhost/user/project/index.php?q=system
Instead of getting any results I just get a typical 404 error.
I've also just checked to see if mod_rewrite works by replace my .htaccess code with this:
Options +FollowSymLinks
RewriteEngine On
RewriteRule (.*) http://www.stackoverflow.com
And it properly redirects me here, so mod_rewrite is definitely working.
The root path to my project is /home/user/public_html/project
The the url used to view my project is http://localhost/user/project
If anymore information is required let me know.
Thanks
If your .htaccess file is indeed located in the project/ subdirectory already, then don't mention it in the RewriteRule again. Remove it:
RewriteRule ^([0-9]{4})/([0-9]{2})$ /project/index.php?q=$1&r=$2 [L]
# no "project/" here
Rules always pertain to the current local filename mapping.
Else experiment with a RewriteBase.
You have [0-9]{4} in your regex which will only match numbers of 4 digits. "system", however, is not a number of 4 digits, and therefore does not match.
You can use something like [^/]+ instead.
RewriteRule ([^/]+)/([0-9]{2})$ /index.php?q=$1&r=$2 [L]
RewriteRule ([^/]+)$ /index.php?q=$1 [L]
Don't know if the second parameter should be a number with 2 digits or not.
Edit: I also added "user" at the beginning now.
Edit2: Okay, I thought you were in the root htdocs with your htaccess. So remove "project" and "user" if you are in "project" with the .htaccess.
You probably mean
RewriteRule ^/project/([0-9]{4})/([0-9]{2})$ /project/index.php?q=$1&r=$2 [L]
RewriteRule ^/project/([0-9]{4})$ /project/index.php?q=$1 [L]
The '^project' means "start of line is 'project'" but the start is a '/project', so you need to include the starting slash (i.e. '^/project...').
Sorry, missed the system bit (and the user bit). Was concentrating on the slash.
RewriteRule ^/user/project/([a-zA-Z0-9]*)/([a-zA-Z0-9]*)$ /user/project/index.php?q=$1&r=$2 [L]
RewriteRule ^/user/project/([a-zA-Z0-9]*)$ /user/project/index.php?q=$1 [L]
Should have you right.

Categories