What's the code should behind this situation? - .htaccess

This is the second time I'm asking here about it. I shall try to make it easier to give you the answer.
This is all about rewriteing rule. First of all, have a look below:
The following 4 php script is related to account managing of my php login script.
account.php
login.php
signup.php
logout.php
More details about the scripts:
Accout.php has 4-5 pages based on GET request. E.G. ?page=edit, ?page=view etc.
Login.php has only 2 pages, ?action=login and ?action=block.
Signup.php has 3 pages, ?page=process, ?page=process&step=1,2.
logout.php has only one page.
That's a part of my script. Now I'm interested to rewrite the ugly url. I want all the page has 'account' prefix.
account/edit
account/view
account/login
account/login/blocked
account/create
account/create/step/1
account/create/step/2
account/logout
I think I have cleared my question as much as I can. I have tried a lot but get failed. I hope you guys have the answer for me for "How could I do that?" or "What's the code should behind this situation?"
Please, I really need your help badly.
[Edit]
Thanks for your support #JonLin. Thanks a lot. But I need one more help from you. I hope if you help here i could do others.
Now i have,
forum.php?fid=1&p=2
thread.php?tid=1&p=2
(its not only 1 or 2, its [(0-9)+] )
How to do it like this
forum/f1/anything/page-2
forum/t1/anything/page-2
I've tried your signup.php code with a little edit, but its not working.
All of my pages of the script is formatted like this, any.php?xid=n&p=n ( n != 1 or n > 1 ).
So i want the same prefix used for any.php, that related to the any.php, for i.e. any.php has a book.php i want the url like this, any/a1/anything/page-2 and any/b1/anything/page-2 ( page-n will be shown when n > 1 )
Please help me here, I am stuck here. Thank you very much for your friendly help.

RewriteEngine On
# to account.php
RewriteRule ^account/edit/?$ /account.php?page=edit [L]
RewriteRule ^account/view/?$ /account.php?page=view [L]
#etc...
# to login.php
RewriteRule ^account/login/?$ /login.php?action=login [L]
RewriteRule ^account/login/blocked/?$ /login.php?action=block [L]
# to signup.php
RewriteRule ^account/create/? /signup.php?page=process [L]
RewriteRule ^account/create/step/([12])/?$ /signup.php?page=process&step=$1 [L]
# to logout.php
RewriteRule ^account/logout/?$ /logout.php [L]

Related

have a unique number in the url - do not match

This is how I would like to get my url to look at it any other way than before.
The problem is such that I must have forum and after forum content unique number that is in the database and after the url of the content.
I could imagine that it looked like this:
/forum/1/hello-world-stackoverflow-danmark/
now on my .htaccess file:
RewriteRule ^forum/([^/.]*)/([^/.]*)/?$ /forum-s.php?id=$1&url=$2 [L]
and now it:
www.hello-world.com/forum/velkommen-til-traenigsmakker---sjaelland/
I will have its to
www.hello-world.com/forum/1/velkommen-til-traenigsmakker---sjaelland/
Be sure to have RewriteEngine On
The code you have should work, you can optimize it like this to ensure the numbers.
RewriteRule ^forum/([0-9]+)/([^/.]*)/?$ /forum-s.php?id=$1&url=$2 [L]

.htaccess rewrite rule - custom structure

Is it possible to create a .htaccess rule which will take the middle of a URL structure, but resume the normal REQUEST_URL (Sorry for my terrible explanation)
Take this URL for example
/boats/283/manage/water
Now let's say I'm keeping the hierarchy standardisation as per the URL structure, minus the ID (ID in this case is 287) - so the actual script location is /boats/manage/water(.php)
But obviously I don't have to have a manual rule for each page, as that will get tedious.
eg (What I want to avoid per page).
RewriteRule ^boats/(\d+)/manage/water$ ./boats/manage/water.php?id=$1
RewriteRule ^boats/(\d+)/manage/bacon$ ./boats/manage/bacon.php?id=$1
I have no doubt I could find something relevant in Google, but I just can't quite come up with the proper keywords..
Any help/push in the right direction is much appreciated :)
You can try:
# group out the first path, the ID, then the rest
RewriteCond %{REQUEST_URI} ^/([^/]+)/(\d+)/(.*)$
# pre-check that the destination php file actually exists
RewriteCond %{DOCUMENT_ROOT}/%1/%3.php -f
# rewrite
RewriteRule ^ /%1/%3.php?id=%2 [L]

.htaccess work with and without trailing slash

Using .htaccess, how can I get my website to be able to do this. Here is what I have already.
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)/(.*)$ sites/index.php?url=$1&page=$2
From this, I am able to be able to browse to:
http://domain.com/sites/example/
and that works totally fine.
What I want to be able to do is browse to
http://domain.com/sites/example
with no trailing slash, but it always gives back an error with page not found.
So far from Google, all of the results have failed me.
Thank you.
Try changing your regex to:
RewriteRule ^([^/]+)/(.*?)/?$ sites/index.php?url=$1&page=$2
The first (.*) is greedy, and will gobble up everything.
Yeah the sea of info in google can sometimes be a hinderance:
RewriteRule ^(.*)/(.*)/?$ sites/index.php?url=$1&page=$2
Note the /? in the rewriterule before the $, sorry I've read your question backwards...
edited answer:
RewriteRule ^(.*)/?(.*)$ sites/index.php?url=$1&page=$2
The different position of the ? might help as otherwise the greedy match of the first(.*) might glob to the end and try and shove that all into $1, for the specifics you'd be best turning on the apache rewrite log and looking at that if you can.

.htaccess Redirect sub-folder

Hi I'm not a programmer by any stretch of the imagination and am trying to do a multi 301 redirect in my htaccess file based on the following:
So I have a ton of urls all with similar naming conventions - here is a sample of 2.
http://www.hollandsbrook.com/garrett-at-gold/
http://www.hollandsbrook.com/garrett-ace-250/
These urls need to redirect to:
http://www.hollandsbrook.com/garrett-metal-detectors/garrett-at-gold/
http://www.hollandsbrook.com/garrett-metal-detectors/garrett-ace-250/
I could just redirect them 1 line at a time, but I'd like to use regex.
Here's what I was thinking so far but not working:
RewriteRule ^garrett-([a-z])/$ /garrett-metal-detectors/$1/ [R]
Basically i need to redirect any page right off the root that starts with "garrett-" to include the folder path of "garrett-metal-detectors".
Any thoughts would be MUCH appreciated. Many thanks in advance for your help.
if you want temprorary redirect use:
RewriteRule ^garrett\-([a-z0-9\-]+)/?$ /garrett-metal-detectors/garrett-$1/ [R=302,L]
if you want permanent redirect use:
RewriteRule ^garrett\-([a-z0-9\-]+)/?$ /garrett-metal-detectors/garrett-$1/ [R=301,L]
I'm am not an expert on Regular Expressions, but looks like your reg ex may be a bit off...
try:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^((garrett)(-[a-z0-9]).*)/$ /metal-detectors/$1/ [R]
This is looking fro anything starting with "garrett" followed by any letter/number/hyphen combo.
Note: having "garett" in the destination part give you a loop of redirects, so you may have to choose a different word, or remove it all together...

agh - mod_rewrite remove '-xxx-XXX' from URL

bit of a nightmare. Clients sent out a big email with in links in them which have subsequently changed!
just need to change
forthcoming-events/event/skills-xxx-XXX
to
forthcoming-events/event/skills
so just removing the '-xxx-XXX'. Any help majorly appreciated as struggling to find a solution.
I'd have thought that something like:
RewriteRule \A/forthcoming-events/event/skills-xxx-XXX/*$ /forthcoming-events/event/skills/$1 [L]
...would do the trick. (That said, I've presumed that the "skills-..." bit is a directory.
RewriteRule ^(.*)-xxx-XXX$ $1 [R=302,NC]
the above works to match the rewrite the URLs!
cheers...

Resources