Welcome to WebmasterWorld Guest from 18.212.92.235

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

Struggling with a simple redirect

     
11:39 am on Jun 12, 2015 (gmt 0)

Junior Member

10+ Year Member

joined:June 26, 2008
posts:178
votes: 0


Hello,

I recently decided that I wanted all of our URLS to have a training slash, so I adjusted some of my rejex and the PHP to suit.

However, whilst the page loads when you click into it, after form submissions, redirecting at the header level using location: is having none of it.

I've tried everything at the PHP level and I've been coming back to this all week but I'm getting nowhere.

I was hoping that someone could take a look at my regex and give me some insight as to where I might be going wrong.

# Restructure URLS in the enquiry/ folder.
# folder/index.php?id=123 becomes folder/123/
RewriteRule ^enquiry/details/([0-9]+)/?$ enquiry/details/index.php?id=$1 [L]
RewriteRule ^enquiry/company/([0-9]+)/?$ enquiry/company/index.php?id=$1 [L]
RewriteRule ^enquiry/parts/([0-9]+)/?$ enquiry/parts/index.php?id=$1 [L]
RewriteRule ^enquiry/quotation/([0-9]+)/?$ enquiry/quotation/index.php?id=$1 [L]
RewriteRule ^enquiry/lost/([0-9]+)/?$ enquiry/lost/index.php?id=$1 [L]
RewriteRule ^enquiry/activity/([0-9]+)/?$ enquiry/activity/index.php?id=$1 [L]

Thank you!

Tom.
1:47 pm on June 12, 2015 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts: 315
votes: 22


...after form submissions, redirecting at the header level using location: is having none of it.


Maybe a bit of an aside, but worth clarification I think.... why are you "redirecting" at this stage? The form submission should go straight to the "pretty URL" (with trailing slash) and your mod_rewrite directives internally rewrite it.

And what does "is having none of it" mean? What actually happens? What URL are you (wanting to) going from / to?

# folder/index.php?id=123 becomes folder/123/


Your comment is a bit misleading here, your code does the opposite.

You also state that you want a "trailing slash", but your RewriteRule pattern makes this optional. Do you have other "folders" that fit this pattern that you don't want rewritten? As your rules could be simplified to just one? For example:


RewriteRule ^enquiry/([a-z]+)/([0-9]+)/?$ /enquiry/$1/index.php?id=$2 [L]


Do you have a RewriteBase directive? SInce you are using a relative path substitution, this would be mandatory.
6:41 pm on June 12, 2015 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15372
votes: 724


SInce you are using a relative path substitution, this would be mandatory.

The default RewriteBase is / for the root: not the current directory (determined by where your rule is located) but the overall hostname. But since the target of a RewriteRule is in your control, it should never be necessary to specify a RewriteBase at all; just make sure they always begin in / .

On the primary question: Adding a / sounds trivial, but it completely changes where the browser "thinks" it is.
example.com/directory/pagename
=
browser thinks it is in /directory/ and all relative links are construed accordingly.

example.com/directory/pagename/
=
browser thinks it is in /directory/pagename/ and all relative links etcetera.

Your quoted rules only show half of the redirect-to-rewrite two-step: how things get rewritten once the user has requested the correct URL. Does the preceding redirect happen strictly within php? If so, I'd expect to see some preliminary rule on the pattern of
RewriteRule {all-old-URLs-here} /fixup.php [L]

My understanding-- which may be wrong and you should check with a PHP guru-- is that you don't need to pass the requested (wrong/outdated) URL to the php at this stage, because it still "knows" what the request was (and will continue to "know" it as long as it's working on the initial request).
8:56 am on June 15, 2015 (gmt 0)

Junior Member

10+ Year Member

joined:June 26, 2008
posts:178
votes: 0


Maybe a bit of an aside, but worth clarification I think.... why are you "redirecting" at this stage? The form submission should go straight to the "pretty URL" (with trailing slash) and your mod_rewrite directives internally rewrite it.

I am redirecting because the form input can affect the final location depending on what they do.

And what does "is having none of it" mean? What actually happens? What URL are you (wanting to) going from / to?

The form submits to itself, then it either redirects back to an index or submits to itself again under a certain circumstance. However, on the later, the page 404s, although, when I echo out the URL rather than redirecting, the location is correct.

You also state that you want a "trailing slash", but your RewriteRule pattern makes this optional. Do you have other "folders" that fit this pattern that you don't want rewritten?

Regex really isn't my strong point. How is it optional? What have I done wrong - because it's not supposed to be optional... Also, lots of folders fit this pattern. Somewhere in the region of 30 folders and I've declared them all indervidually because the folder names are different....

Do you have a RewriteBase directive?

No.......... Everything works as it's supposed to though - well, it did!...

Does the preceding redirect happen strictly within php?

Yeah, all the redirection is performed using PHP or HTML forms.

You guys thinking that this is more of a PHP issue then?

It's odd, because the first time you hit the URL, it's OK. The second time (after the refresh), it breaks... I've tried (what feels like) everything.

Thanks for your help so far!

Tom.
10:18 am on June 15, 2015 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts: 315
votes: 22


However, on the later, the page 404s, although, when I echo out the URL...


You can presumably see the URL in the address bar that is triggering the 404?

How is it optional?


The "?" after an element (a slash in this case) makes that element optional.

You guys thinking that this is more of a PHP issue then?


Maybe you should post you PHP redirection code. (Should a new thread be started in the PHP forum?)

Do you have any other directives in you .htaccess file? The redirection that lucy24 mentions, catching stray requests for the old URL, is handled by PHP as well?

Just to clarify, all you links and everything point to your new URL structure?
11:02 am on June 15, 2015 (gmt 0)

Junior Member

10+ Year Member

joined:June 26, 2008
posts:178
votes: 0


You can presumably see the URL in the address bar that is triggering the 404?

That's what's bugging me - I can't. Perhaps I should adjust my 404 redirection so I can.... I've tried echoing out what the URL should be - and it always looks fine...

The "?" after an element (a slash in this case) makes that element optional.

Thank you - I'll drop that then.

Maybe you should post you PHP redirection code. (Should a new thread be started in the PHP forum?)

Yeah, I'll make a new PHP thread if I haven't sussed it out by the end of the day.

Do you have any other directives in you .htaccess file? The redirection that lucy24 mentions, catching stray requests for the old URL, is handled by PHP as well?

Yes, here is my whole script... (Apologies if it's terrible).

RewriteEngine on

# Restructure the URLS in the following folders as they are all the same.
# /active-enquiries
# /all-enquiries
# /orders
# folder/?statusID=1&pageNo=2 becomes folder/1/2
RewriteRule ^active-enquiries/([0-9]+)/([0-9]+)/$ active-enquiries/index.php?statusID=$1&pageNo=$2 [L]
RewriteRule ^all-enquiries/([0-9]+)/([0-9]+)/$ all-enquiries/index.php?statusID=$1&pageNo=$2 [L]
RewriteRule ^orders/([0-9]+)/([0-9]+)/$ orders/index.php?statusID=$1&pageNo=$2 [L]

# Restructure URLS in the search/ folder.
RewriteRule ^search/([^/\.]+)/([0-9]+)/$ search/index.php?criteriaType=$1&pageNo=$2 [L]

# Restructure URLS in the enquiry/ folder.
# folder/index.php?id=123 becomes folder/123/
RewriteRule ^enquiry/details/([0-9]+)/?$ enquiry/details/index.php?id=$1 [L]
RewriteRule ^enquiry/company/([0-9]+)/?$ enquiry/company/index.php?id=$1 [L]
RewriteRule ^enquiry/parts/([0-9]+)/?$ enquiry/parts/index.php?id=$1 [L]
RewriteRule ^enquiry/quotation/([0-9]+)/?$ enquiry/quotation/index.php?id=$1 [L]
RewriteRule ^enquiry/lost/([0-9]+)/?$ enquiry/lost/index.php?id=$1 [L]
RewriteRule ^enquiry/activity/([0-9]+)/?$ enquiry/activity/index.php?id=$1 [L]

# Restructure URLS in the order/ folder.
# folder/index.php?id=123 becomes folder/123
RewriteRule ^order/details/([0-9]+)/?$ order/details/index.php?id=$1 [L]
RewriteRule ^order/company/([0-9]+)/?$ order/company/index.php?id=$1 [L]
RewriteRule ^order/parts/([0-9]+)/?$ order/parts/index.php?id=$1 [L]
RewriteRule ^order/documents/([0-9]+)/?$ order/documents/index.php?id=$1 [L]
RewriteRule ^order/invoice/([0-9]+)/?$ order/invoice/index.php?id=$1 [L]
RewriteRule ^order/order-confirmation/([0-9]+)/?$ order/order-confirmation/index.php?id=$1 [L]
RewriteRule ^order/delivery-note/([0-9]+)/?$ order/delivery-note/index.php?id=$1 [L]
RewriteRule ^order/purchase-order/([0-9]+)/([0-9]+)/?$ order/purchase-order/index.php?id=$1&poid=$2 [L]
RewriteRule ^order/split/([0-9]+)/?$ order/split/index.php?id=$1 [L]

# Restructure URLS in the administration/ folder.
RewriteRule ^administration/view/companies/([a-z0]+)/([0-9]+)/?$ administration/view/companies/index.php?letter=$1&id=$2 [L]
RewriteRule ^administration/view/manufacturers/([a-z0]+)/?$ administration/view/manufacturers/index.php?letter=$1 [L]
RewriteRule ^administration/view/suppliers/([a-z0]+)/?$ administration/view/suppliers/index.php?letter=$1 [L]
RewriteRule ^administration/view/parts/([0-9]+)/?$ administration/view/parts/index.php?limit=$1 [L]
RewriteRule ^administration/edit/company/([0-9]+)/?$ administration/edit/company/index.php?id=$1 [L]
RewriteRule ^administration/edit/branch/([0-9]+)/?$ administration/edit/branch/index.php?id=$1 [L]
RewriteRule ^administration/edit/contact/([0-9]+)/?$ administration/edit/contact/index.php?id=$1 [L]
RewriteRule ^administration/edit/part/([0-9]+)/?$ administration/edit/part/index.php?id=$1 [L]
RewriteRule ^administration/edit/supplier/([0-9]+)/?$ administration/edit/supplier/index.php?id=$1 [L]
RewriteRule ^administration/edit/manufacturer/([0-9]+)/?$ administration/edit/manufacturer/index.php?id=$1 [L]
RewriteRule ^administration/edit/user/([0-9]+)/?$ administration/edit/user/index.php?id=$1 [L]
RewriteRule ^administration/add/company/([0-9]+)/?$ administration/add/company/index.php?enquiryID=$1 [L]
RewriteRule ^administration/add/branch/([0-9]+)/?$ administration/add/branch/index.php?enquiryID=$1 [L]
RewriteRule ^administration/add/contact/([0-9]+)/?$ administration/add/contact/index.php?enquiryID=$1 [L]

# Restructure URLS in the knowledge-base/ folder.
RewriteRule ^knowledge-base/manufacturers/([a-z0]+)/([0-9]+)/?$ knowledge-base/manufacturers/index.php?letter=$1&id=$2 [L]
RewriteRule ^knowledge-base/suppliers/([a-z0]+)/([0-9]+)/?$ knowledge-base/suppliers/index.php?letter=$1&id=$2 [L]
RewriteRule ^knowledge-base/parts/([0-9]+)/?$ knowledge-base/parts/index.php?id=$1 [L]

# Restructure URLS in the reporting/ folder.
# folder/index.php?id=123 becomes folder/123
RewriteRule ^reporting/manufacturer-sales/breakdown/([0-9]+)/?$ reporting/manufacturer-sales/breakdown/index.php?manufacturerID=$1 [L]

# Remove the .php file extensions.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]

# Stop people from seeing empty indexes.
Options -Indexes

# Only allow people onto ----- from within the main office.
AuthType Basic
AuthName "---- Requires Authentication"
AuthUserFile "/home/----/.htpasswds/public_html/----/passwd"
Require valid-user
Order deny,allow
Deny from all
Allow from xx.xx.xx.xx
Allow from xx.xx.xx.xx
Satisfy any


Just to clarify, all you links and everything point to your new URL structure?

Yeah, I've tried pretty much every way of approaching it too.
12:08 pm on June 15, 2015 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts: 315
votes: 22


(I seem to have a problem with my R's!?)


# Remove the .php file extensions.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]


This is potentially a problem. In fact, how do you get a 404 response at all? This would seem to result in a rewrite-loop if the file was not found? I think you should only rewrite if the rewritten request would exist, with an additional condition like...


RewriteCond %{REQUEST_FILENAME}.php -f
1:08 pm on June 15, 2015 (gmt 0)

Junior Member

10+ Year Member

joined:June 26, 2008
posts:178
votes: 0


That's always worked for me...

I've resolved the problem now though and it wasn't regex related afterall - but it was your input that helped.

I altered the 404 pages so there wasn't a redirection and so that I could see the URL and I discovered that one of the page includes was redirecting again based on something else. I'd forgotten about this!... All this and it was a silly user error.

Thank you for your help!
4:11 pm on June 15, 2015 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15372
votes: 724


Picking one at random:
# folder/?statusID=1&pageNo=2 becomes folder/1/2
RewriteRule ^active-enquiries/([0-9]+)/([0-9]+)/$ active-enquiries/index.php?statusID=$1&pageNo=$2 [L]

The quoted rule shows how a request for /active-enquiries-pretty-URL is rewritten to show content from active-enquiries-ugly-URL. (But please, please, put a slash at the front of each target.) Where's the rule that redirects explicit requests for /active-enquiries-ugly-URL? A rule that creates an external redirect has to be located before rules creating an internal rewrite (going by final result of rule, not just by flag). Even if your internal links are up-to-date, you still need to deal with old requests, whether from bookmarks or search engines.

I altered the 404 pages so there wasn't a redirection

This line makes me uneasy, because 404 and redirects should not have anything to do with each other.
7:47 am on June 16, 2015 (gmt 0)

Junior Member

10+ Year Member

joined:June 26, 2008
posts:178
votes: 0


So what do you mean, Lucy? Rewriting the rule to look like this?

RewriteRule ^/active-enquiries/([0-9]+)/([0-9]+)/$ active-enquiries/index.php?statusID=$1&pageNo=$2 [L]


As for the 404's, it was poorly worded on my part. I had simply defined the 404 page in the .htaccess and by doing that, I couldn't see the incorrect URL in the browsers URL bar.
4:40 pm on June 16, 2015 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15372
votes: 724


I had simply defined the 404 page in the .htaccess and by doing that, I couldn't see the incorrect URL in the browsers URL bar.

There's something wrong with your ErrorDocument directive, then. An ErrorDocument line must never include protocol-plus-domain ("http://www.example.com/") or the error will turn into a 302 redirect. You don't want that. Instead it should look like
ErrorDocument 404 /missing.html
or
ErrorDocument 404 /boilerplate/errordocs/missing.html
The path can be as long as you like, so long as it starts from the root.

RewriteRule ^/ etcetera

A rule in this form will always fail in htaccess. (Also in a <Directory> section of the config file.) You have to leave off the opening slash.

:: rewind ::

Oh, you misunderstood what I said before. The stuff on the left -- immediately after "RewriteRule" -- is the "pattern". That's what your rule matches against. The stuff on the right -- immediately before the flags -- is the "target". That's what you're redirecting or rewriting to. And that's where you need a leading slash each time.