mod_rewrite: multi-level URL using a request parameter - php

I currently have this set up and working fine inside a users folder.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?i=$1
e.g 127.0.0.1/site/users/admin goes to 127.0.0.1/site/users/index.php?i=admin
Now as this is for a profile page, how would i do something such as this.
users/admin/activity
So that it would show the activity page for that user? I am totally confused on how i would go about this.
Would it be best to make the index.php accept a page $_GET variable? But the how would i get htaccess to work around it?

You rules will look something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.*)$ index.php?i=$1&page=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?i=$1 [L]
Now your index.php should be getting 2 $_GET variables, i and page.

Related

Multiple variables in .htaccess

i want to make my URL become from this:
www.mysite.com/index.php?area=test1&par=test2
to this:
www.mysite.com/test1/test2
my .htaccess look like that:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*?)/?$ /index.php?area=$1 [L]
DirectoryIndex index.php
i get only the area variable, and dont know how to continue from here.
why not simply pass all to php like
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
than process it in php
var_dump($_SERVER);//parse all info you want

.htaccess code showing 500 internal error

Hi there I'm trying to make a blog demo and I'm having some pretty URL codes already.
i am having a url www.xyz.com
and a search url www.xyz.com/search/this+is+a+search+text
in search url the parameter search is a page name and this+is+a+search+text is a parameter that i'll be parsing
I'm having a .htaccess code below already
# code to make pretty URLS | we're using this code to achieve /category/slug
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/post.php?&category=$2&page=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)/([\w-]+)$ app/post.php?&category=$2&slug=$3 [L,QSA]
# code to make pretty URLS for search page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/search.php?&searchstring=$2&page=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\d]+)$ app/index.php?page=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)$ app/post.php?category=$2 [L]
I'm using the below code for serach page
# code to make pretty URLS for search page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/search.php&searchstring=$2&page=$3 [L,QSA]
But while using the code I get a 500 internal error message, I'm not able to figure out what's the error!
I would really appreciate if anyone could help me out with this logic.
Rewrites in achieve /category/slug and for search page are the same. Search request matched with first - achieve rewrite and run app/post.php...
To answer your followup question to JarekBaran
JarekBaran: Rewrites in achieve /category/slug and for search page are the same.
What this means is that the RewriteCond are the same for the pretty URL code for the caterogy/slug page and the search page.
This means that the first match will always be used and so:
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/post.php?&category=$2&page=$3 [L,QSA]
will always be triggered before
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/search.php?&searchstring=$2&page=$3 [L,QSA]
This way you will not be able to use the search function. You could consider adding a parameter to identify if a search is made or not.
Your problem is that you are starting your rules with (.+) which will match one or more of anything. This means that you are also matching the / character. Some of your 2 parameter redirects are matching your urls with 3 parameters because of this. You would be better of starting with something like this - ([a-zA-Z0-9-]+)
Your search rewrite should probably be something like this - RewriteRule ^/?([a-zA-Z0-9-]+)/([\w+]+)/([\d]+)$ app/search.php?&searchstring=$2&page=$3 [L,QSA]
I tested that rewrite on this htaccess testing tool and it is working.
That said your other rules will need to be changed as well since they all start with (.+). After doing that your category rewrite will conflict with your search rewrite.
However, this would probably work for you:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?category/([\w-]+)/([\d]+)$ app/post.php?&category=$2&page=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?category/([\w-]+)/([\w-]+)$ app/post.php?&category=$2&slug=$3 [L,QSA]
# code to make pretty URLS for search page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?search/([\w+]+)/([\d]+)$ app/search.php?&searchstring=$2&page=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([a-zA-Z0-9-]+)/([\d]+)$ app/index.php?page=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?category/([\w-]+)$ app/post.php?category=$2 [L]

Changing the URL pattern in PHP

Right now my URL looks like:
https://www.example.com/blogitem.php?id=29
What I want is the user will see the URL in their browser like:
https://www.example.com/blogitem/the-title-of-the-current-blog
This thing is already implemented in Wordpress and other CMS but I am using pure PHP and don't have any idea about how can I implement this feature. Can anyone please help me to implement this?
write this on code .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ blogitem.php?id=$1 [L,QSA]
you need to get title form database instead of id
so it will be like this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ blogitem.php?title=$1 [L,QSA]

MOD_REWRITE but not in sub directories

I have the following HTACCESS code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#RewriteCond %{REQUEST_URI} !(/cms/|/js/|/mobile/)* [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /page.php [L]
As it stands, it works as I need it to within the root of the site, but accessing an admin page (/cms/), I can tell via PhpConsole that it is still hitting page.php. I think this is because the admin is controlled with a query string: URIs look like /cms/?view=pages&action=edit&id=4
If I uncomment the first condition, this problem no longer occurs, but my front end comes back with a 404 on files that don't exist, rather than redirecting to page.php
What is wrong with my code?
Change your code with this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (?!^(cms|js|mobile)(/.*|)$)^.*$ page.php [L,NC]

htaccess behavior when accessing root

I've been using this htaccess code to pass on vars to redirect.php which handles the includes for me:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)/(.*)/(.*)$ redirect.php?value1=$1&value2=$2&value3=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)/(.*)$ redirect.php?value1=$1&value2=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)$ redirect.php?value1=$1 [L,QSA]
but i noticed that it will not go to redirect if all arguments is empty, so if i go to http://domain.com/ it will open index.php, but if i go to http://domain.com/any-param/ redirect.php handles it correctlty. How can I make it always use redirect.php as default, even when no additional URL parameters is set?
Your rules appear to be fine to me. Just add this line in the end:
RewriteRule ^$ redirect.php [L]
This will redirect http://domain.com/ to http://domain.com/redirect.php.
use to do it like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([a-z]+)?/?([a-z]+)?/?([-a-z0-9]+)?/?$ [NC]
RewriteRule ^([a-z]+)/?([a-z]+)?/?([-a-z0-9]+)?/?$ /redirect.php?value1=$1&value2=$2&value3=$3 [NC,L]
Note: Even with only 1 param, this will still work as it just leaves the other 2 blank. So you don't have to copy and paste thew same code while removing just one condition.
I found that this is not very scalable. Now I just redirect everything to the index.php and let that file fetch and dissect the URI to handle my system layout. This allows for a very scalable system.

Categories