Bit of help with a URL rewrite

Hey everyone.

Here is what I want to do…
I have an URL like this:
[B]http://www.domain.com/page/page_name[/B]

and I want to make it look like this:
[B]http://www.domain.com/my_new_name[/B]

This is what I have been trying (i know it worked before):
RewriteRule ^my_new_name/?$ page/page_name [NC]

But, obviously, it doesn’t work.

Thank you in advance for your answers.

two things could be going on:

  1. You forgot “RewriteEngine on”
  2. Your host is running Apache 1.x, in which case your rule should be

RewriteRule [1]/[/COLOR]my_new_name/?$ page/page_name [NC]

Oh, and dont forget the LAST flag, [L], or in you case [NC,L]


  1. COLOR=“Red” ↩︎

loky,

It’s not very often that you actually NEED RewriteEngine on so that’s not likely the case.

It’s also not very likely that you are still on an old installation of Apache (1.x) so that’s not likely the case.

Because the No Case flag is useful to ignore the case of the {HTTP_HOST} (and COULD distort your regex significantly - but not in this case as you’re not capturing the URI and redirecting using the captured name), removing the NC could help - but not likely as well.

IMHO, the problem is most likely to lie with your regex, ergo, please show us the actual RewriteRule you’re using.

BOTH: Using mod_rewrite to redirect like this is an abuse of mod_rewrite. mod_alias’ Redirect is designed for this task and should be used, instead (because, as part of the Apache core, it doesn’t need the regex engine and is, thus, much faster).

Regards,

DK

@ScallioXTX
The RewriteEngine on is on, 2.2.14 is my Apache version and even with L flag is still not working :frowning:

@dklynn
Removing the NC flag makes no difference. Still not working.
The rule I’m using is exactly the one I shoved you above. Here is the Exact rule:
RewriteRule ^repcalc/?$ page/repcalc [NC,L]

And this was the rule I have used before I switched to IP.Board from SMF.
RewriteRule ^repcalc/?$ index.php?page=6 [NC,L,QSA]

And this one really worked.

There are any other things can affect a rule like this one?

Thank you for your answers :wink:

Could you post the complete .htaccess?
Maybe there are rules interfering with this one. As far as I can see your rule in itself is fine.
Or maybe page/repcalc doesn’t exist?

That page does exist…
Here is the entire file:

# Use PHP5 Single php.ini as default
AddHandler application/x-httpd-php5s .php
AddHandler fcgid-script php cgi fcgi

RewriteEngine On
# redirect all non-www traffic
RewriteCond %{HTTP_HOST} ^wowjuju\\.com$
RewriteRule ^.*$ http://www.wowjuju.com%{REQUEST_URI} [R=permanent,L]


#Repcalc rule
RewriteRule ^repcalc/?$ /page/repcalc [NC,L]


#IPB Condidtions
<IfModule mod_rewrite.c>
Options -MultiViews
#RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

#Stop the access here
<Files conf_global.php>
Order allow,deny
Deny from all
</Files>

I did try to remove everything else and leave only the Rewrite on and my nonworking Rule.

Does it work if you move the Options -MultiViews further up in the .htaccess, i.e. above the RewriteEngine on rule?
It might be that MultiViews is hijacking the request before mod_rewrite can work with it.

Nope :frowning: Not working.
Like I said. I have removed everything leaving only my rule…and it is not working :frowning:

Come to think of it, the MultiViews would indeed have nothing to do it I suppose.
But leaving only RewriteEngine on and the Rule in place definitely should work.
What is going wrong exactly?

If I’d know what’s wrong I’d have find the solution :stuck_out_tongue:
When I type in my /repcalc url it’s simply show me the main index.

Then it seems it gets rewritten to index.php

Change


RewriteRule . /index.php [L]

to


RewriteCond %{REQUEST_URI} !^/page/repcalc$
RewriteRule . /index.php [L]

Heh, guess what? Is not working… i hate this.
I’ll try to find a workaround and forget about rewriting.

As DK already said, you should look at mod_alias’ Rewrite
It’s also faster than using a RewriteRule because it doesn’t use regex

FINALLY!
Should have think about this earlier! It is working.

The /page/repcalc was already a FURL. In theory it should have woked but it didnt (dunno why). So, I searched for the “original” URL (in my case this was it: index.php?app=ccs&module=pages&section=pages&page=repcalc)
And made the rule like this:
RewriteRule ^repcalc/?$ index.php?app=ccs&module=pages&section=pages&page=repcalc [NC,L]

NOW is working. Stupid scripts with their strange urls!

Thanks a lot for your help and for your time.
Cheers.

Good to see you got it working. I would have never been able to figure it out with the htaccess alone.
Although I did ask if the page existed, didn’t I … ? :wink:

Yeah but I got it wrong I guess :stuck_out_tongue:
The good thing is that is working!

Thanks again :wink:

loky,

I’m glad that you got it sorted but I should comment on your .htaccess code as that may have helped, too.

[COLOR="RoyalBlue"]# I will put all "CORE" directives before mod_rewrite
# They WILL be processed first anyway so it's just a clarity issue[/COLOR]

# Use PHP5 Single php.ini as default
AddHandler application/x-httpd-php5s .php
AddHandler fcgid-script php cgi fcgi

RewriteEngine On
# redirect all non-www traffic
RewriteCond %{HTTP_HOST} ^wowjuju\\.com$
# RewriteRule ^.*$ http://www.wowjuju.com%{REQUEST_URI} [R=permanent,L]
[COLOR="RoyalBlue"]RewriteRule .? http://www.wowjuju.com%{REQUEST_URI} [R=permanent,L]
# No need to match EVERYTHING - just use fastest regex[/COLOR]

#Repcalc rule
RewriteRule ^repcalc/?$ [COLOR="Red"]/[/COLOR]page/repcalc [NC,L]
[COLOR="Red"]# Leading / in redirection can cause problems[/COLOR]

#IPB Condidtions
[COLOR="Red"]#<IfModule mod_rewrite.c>
# ARGH!  DON'T test for EVERY request - WASTEFUL OF RESOURCES[/COLOR]
Options -MultiViews
#RewriteEngine On
[COLOR="Red"]# RewriteBase /
# BAD! This is used to "undo" mod_alias redirects and you have none![/COLOR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
[COLOR="Red"]# </IfModule>
# Ditto above[/COLOR]

#Stop the access here
<Files conf_global.php>
Order allow,deny
Deny from all
</Files>

Some of that is general technique, some is coding efficiency and some is avoiding senseless repeated CPU cycles. I’m fond of saying that, if you don’t know whether mod_rewrite is enabled or not, you’re NOT a webmaster - hang it up! In other words, know what your code is doing for you and TO Apache, please!

Regards,

DK

Optimization rocks! :stuck_out_tongue:
Thank you for this dklynn.