Redirect URL as same

Hi,

How to redirect a page as the below URL?

If I type http://www.unixforum.co.uk/topic/49817-a-basic-script it take me to http://www.unixforum.co.uk/topic/49817-a-basic-script/

Also http://www.unixforum.co.uk/topic/49817-a-basic-script/sfafajfajbf/ type any duplicate value
it take me again back to same http://www.unixforum.co.uk/topic/49817-a-basic-script/

That is does not accepts any duplicate URL, how to write such redirection?

Hi,

You can try to such redirect with the Apache directive ErrorDocument:
http://httpd.apache.org/docs/2.2/mod/core.html
Sure the directive ErrorDocument will work for you if your website works under Apache. Anyway if HTTP/HTTPS requests are handled by Apache then you need to put something like:
ErrorDocument 404 some_error_document.ext
into .htaccess file located in the document root of your php based website. The error code 404 means “not found”. So the above directive will call “some_error_document.ext” when the error 404 (not found) appears.

ErrorDocument directive doesn’t have to be used only for errors 404, and it can do couple of things except from redirecting your visitor.
Example of ErrorDocument syntax:
ErrorDocument error-code some-error-document-file Here is a snippet with ErrorDocument examples:
1ErrorDocument 500 http://example.com/
2 ErrorDocument 404 /pages/errorDocument/error404.php
3 ErrorDocument 403 “You are forbidden to access this page. How many times should I tell you?”

DQ,

What mod_rewrite code did you use to expect anything other than the URI requested?

Axel - welcome … but please keep on topic. Good and correct information but completely irrelevant to DQ’s question.

Regards,

DK

Actually I use below rewrite code

RewriteEngine on
RewriteRule ^docs/(.*) docs/all-ind.php?type=$1

but I want it to get redirected to correct content as those above URL’s do

like, even if I pass ^docs/(.)/$ or ^docs/(.)/sdfsjdfhjsdfhjshfjsd => It redirects me to same content. how this can be done?

DQ,

That’s why you should show your code: It’s loopy! Don’t you think that docs/{anything} will match docs/all-ind.php? (.*), the :kaioken: EVERYTHING :kaioken: atom is a nice catch-all but you must know how to limit its effect!

The answer to your question is to give mod_rewrite an exit strategy, e.g.,

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^docs/(.*)$ docs/all-ind.php?type=$1 [L]

… will reject the redirection if a filename in the docs folder is requested.

Or

RewriteEngine on
RewriteCond %{QUERY_STRING} !type=
RewriteRule ^docs/(.*)$ docs/all-ind.php?type=$1 [L]

… will reject the redirection if a query string contains a key named type (actually, ending in type).

Regards,

DK

well, here i come with the situation,

RewriteCond %{REQUEST_URI} ^/(.)/(.)/$
RewriteRule ^(.)/(.)/$ /abcpage.php?tech=$2&typ=$1 [L]

I wann the url to be redirected even if the url has / or not in the end

DQ,

Well, your code (please use [noparse]

... 

[/noparse] wrappers to preserve your code for the response) does not do what you say you want to do:

RewriteCond %{REQUEST_URI} ^/(.*)/(.*)/$
RewriteRule ^(.*)/(.*)/$ /abcpage.php?tech=$2&typ=$1 [L] 

First, your RewriteCond is superfluous (redundant, repetitious, duplicates the RewriteRule regex).

Second, your RewriteRule says {anything}/{anything_else}/, i.e., two slashes preceeded by any kind of junk. That is certainly not “has / or not in the end.” An optional trailing / defeats the purpose of having a trailing / (because it’s matched by the (.*)). If you want to make that trailing / optional:

  1. Use /? to specify zero or one of the preceding character, i.e., the /

and

  1. You will be forced to use either <base> tags in your scripts or absolute links (because relative links will have to be to two directory levels which cannot happen - at least not without duplicating files which is ridiculous).

Now, back to your original post:

The first thing you said there was that you wanted to add a trailing /. Is that still the case? Can you handle making your css, js, jpg, gif, etc files into directories (MultiViews)?

The second thing you said you want is more garbage after the topic/49817-a-basic-script/ in your URI. Is that still the case?

What I’m getting at here is called Specificity, making sure that you can verbalize what you want mod_rewrite to do (so you can translate that to code). Since I can’t determine what you want (because of conflicting statements), I can’t even begin to suggest pseudo code for you to convert into code.

Regards,

DK