htaccess multiple URL shortner - php

I currently have a shortner for a profile page. I now want to add an additional shortner to a tag page.
CURRENT CODE (for profile - works fine):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /cd/profile.php?username=$1
My problem is when I want to differiantiate the pattern to add the tag page:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/()$ dh/profile.php?username=$1 [NC,L]
RewriteRule ^tag/()$ cd/tags.php?tag_name=$1 [NC,L]
Now, neither works. I am testing it using XAMP and my domains at the moment would be:
1) Directory for profile --> localhost/cd/profile.php?username=John
2) Directory for tags --> localhost/cd/tags.php?tag_name=blue
Do I have to restructure the root to each page? How can I make it work?
Cheers

Your regex are incorrect in both rules.
Try this code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^user/(.+)$ /dh/profile.php?username=$1 [NC,L,QSA]
RewriteRule ^tag/(.+)$ /cd/tags.php?tag_name=$1 [NC,L,QSA]
Also remember that RewriteCond is only applicable to the very next RewriteRule.

Related

How to make dynamic directories and subdirectories via .htaccess

I am trying to create an htaccess script that will create directories for either zero, one, or two variables.
My file that is being used accepts the following 2 get variables. make and model.
This is a 3 step page. I currently have the page located at at /new.php. This page allows a user to select the first variable (in my case, a vehicle make). By selecting a make on this page the user is taken to /new.php?make=Acura. This page now displays a list of all Acura models. From here, the user can click a model, and they will be directed to /new.php?make=Acura&model=TLX. They can now choose a submodel and will be taken to an information page.
So I'm trying to get:
new.php to go to /new/
new.php?make=XMake to go to /new/XMake/
and new.php?make=XMake&model=XModel to go to /new/XMake/XModel/
This is as far as I have gotten with my code:
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^new/(.*)$ new.php?make=$1 [L,NC]
However any variables I add after this seem to break the first directory? Why is this?
You can use these rules in your site root .htaccess:
Options -MultiViews
RewriteEngine On
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^new/([\w-]+)/([\w-]+)/?$ new.php?make=$1&model=$2 [L,NC,QSA]
RewriteRule ^new/([\w-]+)/?$ new.php?make=$1 [L,NC,QSA]
RewriteRule ^new/?$ new.php [L,NC]
The order of the rules is important. With this rule at the beginning, any request /new/, /new/XMake or /new/XMake/XModel/ matches, and the following rules are ignored.
To match the other rules, the more specific must come first, e.g.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^new/(.+?)/(.+)$ new.php?make=$1&model=$2 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^new/(.+)$ new.php?make=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^new/$ new.php [L,NC]

Rewriting rules in will send me to different page than requested

I have the following rewriting rules:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+Category\.php\?Category=([^\s&]+) [NC]
RewriteRule ^ /C/%1? [R=301,L]
RewriteCond %{THE_REQUEST} \s/+product\.php\?id=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)?$ product.php?id=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^C/([^/]+)/?$ Category.php?Category=$1 [L,QSA]
the redirect work fine if you work from the home page, however when click trough to a product page on the /C/Category Name page it cannot find the product page. It is searching like so
http://localhost/C/product.php?id=302-Product-name
I presume that is because it thinks you are in a different folder.
Not sure how to solve this.
I needed to add the <base href="/" /> so it will send all the links back to the root.
Thanks so much again #anubhava from previous questions

htaccess removing extension in two different pages

i used the htacess to remove the php extension and it worked but i also have a page called profile wich i shorten but that make the pages names being taken as a a profile user name, is there a way to fix this?
the results i want is like this:
localhost/path_folder/index.php to localhost/path_folder/index
and
localhost/path_folder/profile?username=name to localhost/path_folder/name
and my htaccess code for the removing extension is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC]
and the code for the profile page is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /path_folder/profile.php?username=$1
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
## First try To internally redirect /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ /path_folder/$1.php [L]
## if PHP file not found then load profile page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ /path_folder/profile.php?username=$1 [L,QSA]
If you want to remove file extension
RewriteEngine On
RewriteRule ^path_folder/([^/]*)$ path_folder/$1.php [L]
Shorten URL for userprofiles
RewriteEngine On
RewriteRule ^path_folder/profile/([^/]*)$ path_folder/profile?username=$1 [L]

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.

Mod Rewrite and PHP

I am trying to redirect pages using mod_rewrite to the pages with some variables (for using them with PHP's $_GET).
To give an example with few lines:
When the user enters /c/stg it redirects to item_show.php?id=$1&f=1 (where f is page number).
When the user enters /c/stg/2 it redirects to the second page with show.php?id=$1&f=$2.
I think there are no errors or misuses of these in my file but, here's what I want:
I want user to enter /string directly to go item_show.php?id=$1&f=1 with $1 is our string of course...
But when I change my file by removing the /c/ part from RewriteRule it starts giving errors in all other directories and doesn't read any files (my.css) even though I have already defined a RewriteCond %{REQUEST_FILENAME} !-f...
Do you have any suggestions?
Or how can I made this system possible with any method?
Options FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#user
RewriteRule ^u/([^/]+)/?$ profile.php?username=$1 [L,NC]
#marked
RewriteRule ^marked/([^/]+)/?$ item_marked.php?id=$1 [L,NC]
#content
RewriteRule ^c/([^/]+)/?$ item_show.php?id=$1&f=1 [L,NC]
RewriteRule ^c/([^/]+)/([^/]+)/?$ item_show.php?id=$1&f=$2 [L,NC]
Am I wrong if RewriteCond only are passed on to the first RewriteRule??
Try this instead:
Options FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#user
RewriteRule ^u/([^/]+)/?$ profile.php?username=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#marked
RewriteRule ^marked/([^/]+)/?$ item_marked.php?id=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#content
RewriteRule ^c/([^/]+)/?$ item_show.php?id=$1&f=1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ item_show.php?id=$1&f=$2 [L,NC]
Cheers,
RewriteCond directives do only belong to the first following RewriteRule directive. So in your case the two RewriteCond direcitves are only applied to the first RewriteRule directive and not to the others.
But you could use this abortion rule to quit the rewrite process if the request can be mapped to an existing file or directory:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
This would cause that following rules are only tested if the request cannot be mapped directly to an existing file or directory.

Categories