Shortlinks in htaccess - php

I have a website where I want realise such scheme:
If user will type website/user1 I want to redirect user to website/profile.php?user=user1
But I want to redirect website/feed to website/action.php
I changed my .htaccess file to this:
RewriteRule ^([a-zA-Z0-9_\-]+)(/?)$ profile.php?user=$1 [L,QSA]
RewriteRule ^feed(/?)+$ action.php [L,QSA]
When I type website/user1 it works, but not works for website/feed, it seeks for website/profile.php?user=feed

You have to change the order of your rules:
RewriteRule ^feed(/?)+$ action.php [L,QSA]
RewriteRule ^([a-zA-Z0-9_\-]+)(/?)$ profile.php?user=$1 [L,QSA]
It means that if your url matches to the first RewriteRule it will apply it. Otherwise it will go to the next RewriteRule.
From documentation:
The order in which these rules are defined is important - this is the order in which they will be applied at run-time.

Related

.htaccess ReWrites cancelling each other out ... Any Ideas?

I have a couple of rules in my .htaccess file in order to make the URLs a bit cleaner, however, they seem to be cancelling each other out.
The first rule is just to remove the .php from page names,
example : mysite.com/join rather than mysite.com/join.php
RewriteEngine On
Rewrite Condition : %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
The second rule is to make it easier for Users to share their profiles on my site,
example : mysite.com/user1 rather than the actual URL mysite.com/profile.php?user=user1
RewriteRule ^([_A-Z0-9a-z-+]+)$ profile.php?user=$1 [S=1]
I've been playing round with them, and they essentially cancel each other out - Any ideas on how I can get them both working?
Thanks
The problem you have is that both conditions are almost identical i.e. anything that ^([_A-Z0-9a-z-+]+)$ matches will also be matched by ^([^.]+)$, so someone accessing mysite.com/user1 will get redirected to mysite.com/user1.php since that is the first rule that is encountered, and it has the L flag to prevent processing more rules. To prevent this happening you need to make the rules different, e.g. perhaps require user pages to be mysite.com/users/user1? Then you could write the rules as
RewriteRule ^([^./]+)$ $1.php [NC,L]
RewriteRule ^users/([_A-Z0-9a-z-+]+)$ profile.php?user=$1 [S=1]
Note that you need to add / to the characters not to be matched in the first rule, otherwise it will still match mysite.com/users/user1.
Edit
A couple of other alternatives:
If you were willing to have actions (e.g. join) use URLs such as mysite.com/action/join then you could keep users at the top level e.g.
RewriteRule ^action/([^.]+)$ $1.php [NC,L]
RewriteRule ^([_A-Z0-9a-z-+]+)$ profile.php?user=$1 [S=1]
Or if you know the names of all your actions you could put them in an alternation (this would require that you couldn't have a user called e.g. join):
RewriteRule ^(join|login|logout|delete)$ $1.php [NC,L]
RewriteRule ^([_A-Z0-9a-z-+]+)$ profile.php?user=$1 [S=1]

url mod rewriting multiple parameters

I have these rules that i want to make it work.
RewriteEngine on
RewriteRule ^/(view|show)/(ebook|lecture)/?$ page1.php?a=$1&b=$2 [L,QSA]
RewriteRule ^/(view|show)/(ebook|lecture)/([^/]+)/?$ page2.php?a=$1&b=$2&c=$3 [L,QSA]
RewriteRule ^/([^/]+)/-page-([0-9]+)?$ page3.php?a=$1&b=$2 [L,QSA]
My links are normally like this
www.domain.com/page1.php?a=view&b=ebook
www.domain.com/page2.php?a=view&b=ebook&c=title
www.domain.com/page3.php?a=title&b=6
and i want to turn them like the following look
www.domain.com/view/ebook //page1
www.domain.com/view/ebook/title //page2
www.domain.com/title/page-6 //page3
I've tried my rules but only the first one worked, But the page didn't load and the style was literally broken and not even a single image loaded or anything at all.
Try with:
RewriteEngine on
RewriteRule ^/?(view|show)/(ebook|lecture)/?$ page1.php?a=$1&b=$2 [L,QSA]
RewriteRule ^/?(view|show)/(ebook|lecture)/([^/]+)/?$ page2.php?a=$1&b=$2&c=$3 [L,QSA]
RewriteRule ^/?([^/]+)/page-([0-9]+)?$ page3.php?a=$1&b=$2 [L,QSA]
No leading slashes in left htaccess RewiteRule url (you can use RewriteRule ^(view...)
And you use only page- in your www.domain.com/title/page-6 //page3 link (not -page-)

mod rewrite -new.php to /new

I want to rewrite any page url which contains -new.php to the /new
For example:
example.com/page-new.php
to
example.com/page/new
I am able to remove the ending .php with following rule, but I can't figure out how to match the -new.
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Your rule actually adds .php to the end.
RewriteRule ^(.*)-new\.php$ $1/new [NC,L]
removes -new.php and changes it to /new
EDIT:
After a while of thinking, if you want users to get redirected, use above and add "R=301" to the brackets as third parameter.
OR, if you actually have links in /page/new schema and files in -new.php schema, then you actually wants to remowe /new and add -new.php. If so, then you asked wrong question. Here you go:
EDIT 2:
You also want to redirect /page to page.php. Now you can do this in two ways:
RewriteRule ^(.*)/new$ $1-new.php [NC,L]
RewriteRule ^(.*)$ $1.php [NC,L]
OR
RewriteRule ^(.*)/new$ $1-new [NC]
RewriteRule ^(.*)$ $1.php [NC,L]
First one are two separate rules, first for /new URLs and then for any other.
Second code is like "first change /new to -new, and then apply next rule"
If you want rule to apply only to urls without dots or anything, then replace (.*) with any other rule like for example ([^\.]*)

.htaccess restricting $_GET

I have a project for modification, where I need to post a form with get, but I am unable to get the post value.
after checking I determine that .htaccess causing problem for this, please help me on this so I get the value without affecting the existing functionality of site.
this is .htaccess file
RewriteRule ^/?([a-zA-Z0-9_+-\s+]+)$ ?x=$1 [QSA,L]
RewriteRule ^/?([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)$ ?x=$1&y=$2
RewriteRule ^/?([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)$ ? x=$1&y=$2&z=$3
RewriteRule ^/?([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)$ ?x=$1&y=$2&z=$3&u=$4
RewriteRule ^/?([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)$ ?x=$1&y=$2&z=$3&u=$4&v=$5
RewriteRule ^/?([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)$ ?x=$1&y=$2&z=$3&u=$4&v=$5&w=$6
RewriteRule ^/?([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)$ ?x=$1&y=$2&z=$3&u=$4&v=$5&w=$6&a=$7
RewriteRule ^/?([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)$ ?x=$1&y=$2&z=$3&u=$4&v=$5&w=$6&a=$7&b=$8
eg : http://www.exampledomain.com/result?term=sampleterm
in this the $_GET['x'] value is result, but I am unable to get term value
You actually need QSA flag (Query String Append), and its better to use L (Last) flag also.
RewriteRule ^/?([\w\s+-]+)/?$ ?x=$1 [QSA,L]
RewriteRule ^/?([\w\s+-]+)/([\w\s+-]+)/?$ ?x=$1&y=$2 [L,QSA]
RewriteRule ^/?([\w\s+-]+)/([\w\s+-]+)/([\w\s+-)/?$ ?x=$1&y=$2&z=$3 [L,QSA]
RewriteRule ^/?([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/?$ ?x=$1&y=$2&z=$3&u=$4 [L,QSA]
# rest of the rules here...
QSA flag makes sure to append existing query string in the new query parameter you're adding through your rules.

Use htaccess for a redirect

I am using the following code to change all my pages from .php to a simple /
RewriteRule ^(.*)/$ $1.php [L]
Now come up as www.site.com/clubs/ or www.site.com/training/ instead of www.site.com/clubs.php and www.site.com/training.php
Now within clubs i want to show the clubs (using data from a database) and the page will show as www.site.com/clubs/my-club/
All the my-club data will be in the database including the link in a url field.
I can get this with the following code. However when i echo using the $_GET['club'] method it is showing up my-club.php instead of just my-club.
RewriteRule ^clubs/([^/]*)$ clubs.php?url=$1 [L]
The ending / in the url seems to get changed into .php
You can achieve what you need in different ways, however that's how I would do it.
Change rules order, like this:
RewriteRule ^clubs/([^/]*)/$ clubs.php?url=$1 [L]
RewriteRule ^(.*)/$ $1.php [L]
I added the trailing slash to the first rule, since I guess is part of the pattern. If I'm wrong about it you can just delete it.
If you want to append any query string from the original request URL you should use QSA together with L, so [L,QSA]
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z\-]+?)/([0-9a-zA-Z\-]+?)?$ $1.php?url=$2
Whan are you run:
http://localhost/club/
result will be
http://localhost/club.php
Whan are you run:
http://localhost/club/my-club
result will be
http://localhost/club.php?url=my-club

Categories