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

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...

Related

What's the code should behind this situation?

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]

.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 to new url problem

I would like to redirect any php page with optional parameter to a new clean url
eg, from: account.php?id=156 to newurl/account/156
I'm using the following redirectmatch
RedirectMatch ^/cmstut/([a-zA-Z0-9_]+)[\.php]?[\?]?[a-zA-Z_]?[=]?([0-9]+)?$ /cmstut/redirect/newurl/$1/$2/$3 [L]
but the result I get is it will redirect to newurl/account//?id=156
I thought it was funny when I read somewhere where htaccess and regular expression was compared to voodoo :) we'll now I understand why
I don't understand where your third subexpression is.
$1: ([a-zA-Z0-9_]+)
$2: ([0-9]+)
$3: MIA
May I recommend something more like this?
/cmstut/([A-Za-z0-9]+)\.php(\??id=([0-9]+))?
then you may have to use $3 to access the id number. you want the entire parameter to be optional, right?
I never used URL redirects in .htaccess before, but if it's plain regular expressions, this should work:
RedirectMatch ^/cmstut/([a-zA-Z0-9_]+)\.php(\?[a-zA-Z_]+=([0-9]+))?$ /cmstut/redirect/newurl/$1/$3 [L]
Since you weren't very specific in what you want, I took some guesses. This will redirect foo.php?bar=123 to newurl/foo/123 and will ignore bar.
Edit: Thinking about it, rewriting your regexp for you won't help you in the long term, and no one except you is likely to know exactly what you want. I think a better course of action is pointing you to a regexp guide. Here is one, and it's specifically targeted for mod_rewrite.
I finally found the solution. I did some more research and I used a different approach which I believe was even better then what i was using
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$ [NC]
RewriteRule ^([A-Za-z0-9]+)\.php$ /cmstut/redirect/newurl/account/%1? [R=302,L]

How to make a catch-all handler in an .htaccess file?

I want to create a rule at the end of an .htaccess file that catches everything that failed to match up until then.
How can I do that?
P.S. I've already tried everything :) Actually, I haven't, but it sure seems like it!
Update: Some people are replying with RewriteRule ^.*$ or an equivalent. This does not work! It will match everything including the other 'good' URLs.
There are actually some good answers here already, but you have responded with...
But then this matches everything including the good stuff.
This is because you aren't telling mod_rewrite to stop processing on a match. To do this, use the "L" tag after each rule, which tells mod_rewrite that "If this rule is matched, stop processing any further rules".
RewriteRule ^RSS/([^/\.]+)/?$ rss.php?Page=$1 [L]
You need to put this after EACH rule. Then, when you put the catch all at the end, it will only be hit if no other rule has been matched.
NOTE: if you are ALSO serving up resources that are not rewritten, like CSS, images, javascript files - you are honestly better off not catching all as you wouldn't want to rewrite their locations.
It sounds like you want to look at the RewriteRule directive. Quie possibly something similar to the following:
RewriteRule ^/(.+) {target}
This will match at the root URL (/) and redirect it to the {target} url.
RewriteRule .* page.php [L] ?

.htaccess rewrite rule

How do I write a rewrite rule for the following condition.
I have my directory setup as
/root/projectname/trunk/www/
I've setup site.local in hosts file to point to /root/
How do I make this request
http://site.local/projectname to grab data from /root/project_name/trunk/www/ instead of from /root/projectname/ ?
Right now I have it as RewriteRule ^projectname/*$ projectname/trunk/www/ [L]
That works for just http://site.local/projectname, but others like http://site.local/projectname/images/image1.jpg doesn't work.
Please enlight.
Thank you,
Tee
I think you should write you rule like following
RewriteRule ^projectname/(*)$ projectname/trunk/www/$1 [L]
but not tested
Ah I figured out what's cracking.
Since the mapping and mapped path both contains projectname, it just keeps on rewriting.
So I got to change the name to something else and it works.
Thanks,
Tee

Resources