URL ShortCut Redirects - php

This is my .htaccess command for making URL shortcuts:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} .(.+)$
RewriteRule ^(.) ?open=encyclopedia&letter=$1&term=%1 [R,NC,L]
With the above works just fine:
example.com/Njumba ➛ example.com/?open=encyclopedia&letter=nj&term=njumba
Now, the problem is that it redirects merely one letter, but not two.
This is how I want it to be:
example.com/NJ ➛ example.com/?open=encyclopedia&letter=nj
Is this also achievable with .htaccess?

You can use this rule instead:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.{1,2}).*$ /?open=encyclopedia&letter=$1&term=$0 [R,NE,QSA,L]

Related

How to use get variables in htaccess (different order)

I am learning PHP and HTACCESS atm.
I have a problem where i use 4 different GET variables. x,y,z,w.
I need to create an URL structure which both can be.
mysite.com/x/y
but also
mysite.com/x/z
and different other combinations.
I
I have tried this in HTACCESS, but it only work if its the same kind of structure, and not with other combnations:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?x=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?x=$1&y=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?x=$1&y=$2&z=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ index.php?x=$1&y=$2&z=$3&w=$4 [L]
Specify [QSA] (Query string append) so you may pass a query string after your url.
Sample Code
RewriteEngine On
RewriteRule ^book/([^/]*)\.html$ book.php?title=$1 [QSA,L]

multiple ht access rules in one file

My site should attempt to serve all requests from a cache subdirectory (/app/storage/cache) such that a request for /two.html is served from /app/storage/cache/two.html
More examples:
/ served from /app/storage/cache/index.html
/two.html served from /app/storage/cache/two.html
/folder served from /app/storage/cache/folder/index.html
If no files/directories are found it should handle all requests with a php file /app/router.php
What I've tried:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /app/storage/www/$1 [L]
RewriteRule ^(/)?$ /app/storage/www/$1 [L]
that works fine for catching all urls and serving the correct resource without changing the url. Now to catch all else:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . app/router.php [L]
That also works, but then the first set of conditions no longer works...
There are three ways I can think of how to go about this. One is check the cache via router.php before generating the page through that same file. The other two ways involve some rewrite-magic.
I have not tested either of these.
Fall-through approach
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /app/storage/www/$1
#From this point on, the file/dir either exists or
#%{REQUEST_FILENAME} is now /app/storage/www/something
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/storage/www/ /app/router.php [L]
Explicit test
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/app/storage/www/$1 !-f
RewriteCond %{DOCUMENT_ROOT}/app/storage/www/$1 !-d
RewriteRule ^(.*)$ /app/router.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /app/storage/www/$1 [L]
Both approaches can likely be sped up by using Apache's main configuration file (httpd.conf) instead of the .htaccess file, as it does not need to search for/read the .htaccess file on every request.

Conflicting rewrite rules htaccess

I have these two rules next to each other
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)?$ category-groups.php?furl=$1 [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)-([^/\.]+)?$ product.php?id=$2&head=$1 [QSA]
This works
/category-groups.php?furl=my-category-page
redirects to
/my-category-page
This doesn't work
/product.php?id=100&head=this-product
does not redirect to
/this-product-100
The product.php page is actually bounced to the category-groups.php page.
I hope that makes sense. I've tried many things but can't figure out to solve it
Thanks
You need to swap the order of your rules and make the regex match numbers:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)-([0-9]+)?$ product.php?id=$2&head=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)?$ category-groups.php?furl=$1 [QSA,L]
otherwise ^([^/\.]+)?$ will always just match everything.

modrewrite help using multiple dynamic gets

original = http://ritetag.techreanimate.com/signin.php?network=twitter&fhfghdfghh=sadfgsdf
rewrite = http://ritetag.techreanimate.com/signin/twitter?fhfghdfghh=sadfgsdf
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^signin/([^/]*)$ /signin.php?network=$1
i got this working but i cant get other variables using get that come after, how do i get toe rewrite to work for stuff other than network.
I solved it!
RewriteEngine On
RewriteBase /
#This rewrite has no L flag so the second one can continue
#Signin.php can have multiple networks like signin/twitter = signin.php?network=twitter
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^signin/([^/]*)$ /signin.php?network=$1 [QSA]
#this rewrite just allows to use files withouth the .php
# Like about instead of about.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [QSA]

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