Jump to content

Recommended Posts

5 minutes ago, SaranacLake said:

And I'm sorry, but I don't see how [L] or [R] or [R=301] or [L,R=301] is related.

They are related because these control what mod_rewrite does after it's re-written the URL.

As has been said multiple times, [R] causes mod-rewrite to instruct the client to re-direct to the new URL.

[L] causes mod-rewrite to stop processing any further re-write rules.

You've been told to remove [R], but keep [L], yet you keep removing both.

1 minute ago, kicken said:

They are related because these control what mod_rewrite does after it's re-written the URL.

As has been said multiple times, [R] causes mod-rewrite to instruct the client to re-direct to the new URL.

[L] causes mod-rewrite to stop processing any further re-write rules.

You've been told to remove [R], but keep [L], yet you keep removing both.

I tried just [L] and that does NOT fix things....

I thought I mentioned that above?

So since just having [L] does not work, that is why I said I don't follow the whole L/R thing...

 

2 hours ago, kicken said:

Also, in case you're still not clear on the [R] issue: A normal RewriteRule without the [R] tells Apache "Hey buddy, I know the client asked for X but let's really give them Y instead, don't tell them about it though."

What does "don't tell anybody though" mean?

So if I remove the [R], then are you implying that in the address bar is the pretty URL, and the browser serves up the ugly URL (which is the web page) but the address retains the pretty URL??

 

 

Quote

If you add the [R], that changes the hypothetical conversation to "Hey buddy, I know the client asked for X but they really want to ask for Y.  Can you please let them know they need to request Y instead?"

What does, "Can you please let them know they need to request Y" mean?

What would I see in the browser and address bar?

In the address bar is the pretty URL.  The browser presumably uses the ugly URL to serve up the web page, right?

What happens to the pretty URL in the address bar when you have [R] ??

 

 

Quote

By adding the 301 into the mix ([R=301]), your modifying the conversation even further to say "Hey buddy, I know the client asked for X but they really want to ask for Y.  Can you please let them know they need to request Y instead and that this will always be the case and they should never request X again?"

Browsers may cache such 301 redirects so that if you attempt to visit the original URL again the browser itself will re-write to the redirected URL before even making the request.  As such, if you make such a redirect by mistake (as you were) it's not easily fixed.  Sometimes closing the browser entirely (not just the tab/window) will clear those redirects, other times you may  have to clear the browsers cache to remove them.

 

Sorry, but a little unsure of what this means...

In the address bar is the pretty URL.  The browser presumably uses the ugly URL to serve up the web page, right?

What happens to the pretty URL in the address bar when you have [R=301] ??

 

My understanding of R=301 was that it just told search engines that the pretty URL was really the ugly URL.

Guess I don't understand that correctly...

 

Edited by SaranacLake

**Note Added: 

When I just have [L] in my mod_rewrite, I get this incorrect URL...

http://local.mydomain/client1/gallery/2019-holiday-party/?gallery-id=2019-holiday-party

 

This part of the above URL is wrong: ?gallery-id=2019-holiday-party

Edited by SaranacLake
45 minutes ago, SaranacLake said:

What does "don't tell anybody though" mean?

It means the server doesn't notify the browser that the URL has been changed.   The server just handles the request using the new URL and returns the result and the browser is non the wiser.  As a result, the browser just shows the original unchanged URL.

46 minutes ago, SaranacLake said:

What does, "Can you please let them know they need to request Y" mean?

It means the server will send a response back to the browser telling it that the URL has changed and it needs to re-request using the new URL.  As a result the browser updates it's address bar and re-requests the new URL.

39 minutes ago, SaranacLake said:

When I just have [L] in my mod_rewrite, I get this incorrect URL...

Then the most likely explanation is that ?gallery-id=2019-holday-party bit is there from the beginning of the request (ie, you're linking incorrectly).

I find when testing rewrites the best thing to do is ignore the browser and instead using something like curl or wget to request the pretty url and see what the response is. 

curl -i http://example.com/client1/gallery/2019-holiday-party

Get yourself a copy of CURL and run the above command and see what it says (post it here if you're unsure what the output means).

58 minutes ago, SaranacLake said:

My understanding of R=301 was that it just told search engines that the pretty URL was really the ugly URL

It does, in a way, by telling the search engine that "The URL you requested (X) has changed to the url (Y) forever, so don't request it again".  Your browser can understand that request as well, it's not a search-engine specific request.

 

I formatted/highlighted what you said so it is clear...

 

10 minutes ago, kicken said:

Scenario #1: mod_rewrite *without* an [R]

It means the server doesn't notify the browser that the URL has been changed.   The server just handles the request using the new URL and returns the result and the browser is non the wiser.  As a result, the browser just shows the original unchanged ("pretty") URL.

 

10 minutes ago, kicken said:

Scenario #2: mod_rewrite with [R]

It means the server will send a response back to the browser telling it that the URL has changed and it needs to re-request using the new URL.  As a result the browser updates it's address bar (with the ugly URL) and re-requests the new URL.

 

 

10 minutes ago, kicken said:

Then the most likely explanation is that ?gallery-id=2019-holday-party bit is there from the beginning of the request (ie, you're linking incorrectly).

I find when testing rewrites the best thing to do is ignore the browser and instead using something like curl or wget to request the pretty url and see what the response is. 


curl -i http://example.com/client1/gallery/2019-holiday-party

Get yourself a copy of CURL and run the above command and see what it says (post it here if you're unsure what the output means).

Hopefully installing cURL on a Mac is fairly idiot proof?

Hopefully using cURL is as simple as pasting your above command into cURL?

 

 

10 minutes ago, kicken said:

It does, in a way, by telling the search engine that "The URL you requested (X) has changed to the url (Y) forever, so don't request it again".  Your browser can understand that request as well, it's not a search-engine specific request.

 

Don't request WHAT again?  The pretty URL?

If so, then what is the benefit to search engines and to your website as far as SEO?

 

I will try to get cURL installed between final daily meetings and get back to you ASAP.

 

And THANKS for helping me to slowly get what [R] does...  😉

 

Any comments on when and why to use [L] ??

 

5 minutes ago, SaranacLake said:

Hopefully installing cURL on a Mac is fairly idiot proof?

Your mac might have it already installed.  Open a terminal window and try running the curl command.

6 minutes ago, SaranacLake said:

Don't request WHAT again?  The pretty URL?

The URL that caused the response (url X).  Given your original rules that would indeed be the pretty URL.

If you create a reverse rewrite to map your ugly URL to your pretty url, then in that instance you'd want to use R=301.

9 minutes ago, SaranacLake said:

Any comments on when and why to use [L]

You use it when you don't want further re-writes to be processed, which is generally the case.  For example, if you had both your pretty URL to ugly URL rewrite (Rule A), and a reverse ugly URL to pretty URL rewrite (Rule B) and didn't include the [L] you'd end up in a loop because Rule A would rewrite the request to the ugly URL.  Then Rule B would re-write the ugly URL back to the pretty URL and redirect the browser which would then request the pretty URL again starting the process over (pretty -> ugly -> pretty -> redirect).

 

 

 

1 hour ago, kicken said:

Then the most likely explanation is that ?gallery-id=2019-holday-party bit is there from the beginning of the request (ie, you're linking incorrectly).

I find when testing rewrites the best thing to do is ignore the browser and instead using something like curl or wget to request the pretty url and see what the response is. 


curl -i http://example.com/client1/gallery/2019-holiday-party

Get yourself a copy of CURL and run the above command and see what it says (post it here if you're unsure what the output means).

 

In Terminal I ran:

	curl -i http://local.mydomain/client1/gallery/2019-holiday-party
	

 

I got these results...

	HTTP/1.1 301 Moved Permanently
Date: Wed, 04 Dec 2019 21:29:01 GMT
Server: Apache/2.2.34 (Unix) mod_wsgi/3.5 Python/2.7.13 PHP/7.2.10 mod_ssl/2.2.34 OpenSSL/1.0.2o DAV/2 mod_fastcgi/mod_fastcgi-SNAP-0910052141 mod_perl/2.0.9 Perl/v5.24.0
Location: http://local.mydomain/client1/gallery/2019-holiday-party/?gallery-id=2019-holiday-party
Content-Length: 297
Content-Type: text/html; charset=iso-8859-1
	<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://local.mydomain/client1/gallery/2019-holiday-party/?gallery-id=2019-holiday-party">here</a>.</p>
</body></html>
	

 

I have no switches (??) after my RewriteRule...

 

So WTF?

 

31 minutes ago, kicken said:

Perhaps you should just post your whole re-write configuration.  Make sure you don't have extra configuration directives in other areas that might cause the problem (httpd.conf, .htaccess files)

Here you go...

	RewriteEngine on
	# Addresses issues with how Apache handles mod_rewrites!!
RewriteBase /
	
# REWRITE INDEX.PHP TO ROOT
#-------------------------------------------------------------------------------
#PRETTY:    www.mydomain.com/
#UGLY:        www.mydomain.com/index.php
	RewriteCond %{REQUEST_URI} ^.*/index\.php 
RewriteRule ^(.*)index.php$ $1 [L,R=301]
	
# SECURITY
#-------------------------------------------------------------------------------
#Prevent Directory Listings
Options -Indexes
	
# ERROR PAGES
#-------------------------------------------------------------------------------
#Handle Access-Denied.
ErrorDocument 403 "/utilities/access-denied.php"
	#Handle Page-Not-Found.
ErrorDocument 404 "/utilities/page-not-found.php"
	
# CLIENT HOME
#-------------------------------------------------------------------------------
#PRETTY:    client1/menu
#UGLY:        client1/menu.php
	#Rewrite only if the request is not pointing to a real file.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .* $0.php [L]
	
# PHOTO-GALLERY
#-------------------------------------------------------------------------------
#PRETTY:    client1/gallery/2019-holiday-party
#UGLY:        client1/gallery/photo-gallery.php?gallery-id=2019-holiday-party
	#Rewrite only if the request is not pointing to a real file.
RewriteCond %{REQUEST_FILENAME} !-f
	RewriteRule client1/gallery/(.*)$ client1/gallery/photo-gallery.php?gallery-id=$1 [L]
	

Appears to work just fine for me if I attempt to re-create what I'm presuming you have on your end.

d:\Users\Keith\Documents\PHP Samples>curl -ik https://samples.test/client1/gallery/2019-holiday-party
HTTP/1.1 200 OK
Date: Wed, 04 Dec 2019 22:55:34 GMT
Server: Apache/2.4.26-dev (Win64) OpenSSL/1.1.0e mod_fcgid/2.3.9
X-Powered-By: PHP/7.2.18
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

<!DOCTYPE html>
<body>
    <p>You are viewing gallery 2019-holiday-party.</p>
</body>
D:.
|   .htaccess
|           
\---client1
    |   menu.php
    |   
    \---gallery
            photo-gallery.php

Is your photo-gallery.php script possibly issuing a redirect?

If you edit your main httpd.conf file instead of a .htaccess file, you can enable rewrite trace logging.

LogLevel rewrite:trace6

This will generate step-by-step logs in your server's ErrorLog file showing how your rules are processed.   You'd get a bunch of entries that look something like this:

[rid#24d13ce2180/initial] [perdir D:/Users/Keith/Documents/PHP Samples/] strip per-dir prefix: D:/Users/Keith/Documents/PHP Samples/client1/gallery/2019-holiday-party -> client1/gallery/2019-holiday-party
[rid#24d13ce2180/initial] [perdir D:/Users/Keith/Documents/PHP Samples/] applying pattern '.*' to uri 'client1/gallery/2019-holiday-party'
[rid#24d13ce2180/initial] [perdir D:/Users/Keith/Documents/PHP Samples/] RewriteCond: input='D:/Users/Keith/Documents/PHP Samples/client1/gallery/2019-holiday-party' pattern='!-d' => matched
[rid#24d13ce2180/initial] [perdir D:/Users/Keith/Documents/PHP Samples/] RewriteCond: input='D:/Users/Keith/Documents/PHP Samples/client1/gallery/2019-holiday-party' pattern='!-f' => matched
[rid#24d13ce2180/initial] [perdir D:/Users/Keith/Documents/PHP Samples/] RewriteCond: input='D:/Users/Keith/Documents/PHP Samples/client1/gallery/2019-holiday-party.php' pattern='-f' => not-matched
[rid#24d13ce2180/initial] [perdir D:/Users/Keith/Documents/PHP Samples/] strip per-dir prefix: D:/Users/Keith/Documents/PHP Samples/client1/gallery/2019-holiday-party -> client1/gallery/2019-holiday-party
[rid#24d13ce2180/initial] [perdir D:/Users/Keith/Documents/PHP Samples/] applying pattern 'client1/gallery/(.*)$' to uri 'client1/gallery/2019-holiday-party'
[rid#24d13ce2180/initial] [perdir D:/Users/Keith/Documents/PHP Samples/] RewriteCond: input='D:/Users/Keith/Documents/PHP Samples/client1/gallery/2019-holiday-party' pattern='!-f' => matched
[rid#24d13ce2180/initial] [perdir D:/Users/Keith/Documents/PHP Samples/] rewrite 'client1/gallery/2019-holiday-party' -> 'client1/gallery/photo-gallery.php?gallery-id=2019-holiday-party'
[rid#24d13ce2180/initial] split uri=client1/gallery/photo-gallery.php?gallery-id=2019-holiday-party -> uri=client1/gallery/photo-gallery.php, args=gallery-id=2019-holiday-party
[rid#24d13ce2180/initial] [perdir D:/Users/Keith/Documents/PHP Samples/] add per-dir prefix: client1/gallery/photo-gallery.php -> D:/Users/Keith/Documents/PHP Samples/client1/gallery/photo-gallery.php
[rid#24d13ce2180/initial] [perdir D:/Users/Keith/Documents/PHP Samples/] trying to replace prefix D:/Users/Keith/Documents/PHP Samples/ with /
[rid#24d13ce2180/initial] strip matching prefix: D:/Users/Keith/Documents/PHP Samples/client1/gallery/photo-gallery.php -> client1/gallery/photo-gallery.php
[rid#24d13ce2180/initial] add subst prefix: client1/gallery/photo-gallery.php -> /client1/gallery/photo-gallery.php
[rid#24d13ce2180/initial] [perdir D:/Users/Keith/Documents/PHP Samples/] internal redirect with /client1/gallery/photo-gallery.php [INTERNAL REDIRECT]
[rid#24d13ce5bb0/initial/redir#1] [perdir D:/Users/Keith/Documents/PHP Samples/] strip per-dir prefix: D:/Users/Keith/Documents/PHP Samples/client1/gallery/photo-gallery.php -> client1/gallery/photo-gallery.php
[rid#24d13ce5bb0/initial/redir#1] [perdir D:/Users/Keith/Documents/PHP Samples/] applying pattern '.*' to uri 'client1/gallery/photo-gallery.php'
[rid#24d13ce5bb0/initial/redir#1] [perdir D:/Users/Keith/Documents/PHP Samples/] RewriteCond: input='D:/Users/Keith/Documents/PHP Samples/client1/gallery/photo-gallery.php' pattern='!-d' => matched
[rid#24d13ce5bb0/initial/redir#1] [perdir D:/Users/Keith/Documents/PHP Samples/] RewriteCond: input='D:/Users/Keith/Documents/PHP Samples/client1/gallery/photo-gallery.php' pattern='!-f' => not-matched
[rid#24d13ce5bb0/initial/redir#1] [perdir D:/Users/Keith/Documents/PHP Samples/] strip per-dir prefix: D:/Users/Keith/Documents/PHP Samples/client1/gallery/photo-gallery.php -> client1/gallery/photo-gallery.php
[rid#24d13ce5bb0/initial/redir#1] [perdir D:/Users/Keith/Documents/PHP Samples/] applying pattern 'client1/gallery/(.*)$' to uri 'client1/gallery/photo-gallery.php'
[rid#24d13ce5bb0/initial/redir#1] [perdir D:/Users/Keith/Documents/PHP Samples/] RewriteCond: input='D:/Users/Keith/Documents/PHP Samples/client1/gallery/photo-gallery.php' pattern='!-f' => not-matched
[rid#24d13ce5bb0/initial/redir#1] [perdir D:/Users/Keith/Documents/PHP Samples/] pass through D:/Users/Keith/Documents/PHP Samples/client1/gallery/photo-gallery.php

 

38 minutes ago, kicken said:

Is your photo-gallery.php script possibly issuing a redirect?

Only for the access-denied.php and page-not-found.php

 

38 minutes ago, kicken said:

If you edit your main httpd.conf file instead of a .htaccess file, you can enable rewrite trace logging.


LogLevel rewrite:trace6

This will generate step-by-step logs in your server's ErrorLog file showing how your rules are processed.   You'd get a bunch of entries that look something like this:

 

 

This is getting really complicated...  ;-(

I am running MAMP.

Went to Applications : MAMP : conf : apache : httpd.conf

 

Searched for "LogLevel"

I see...

	LogLevel error
	

 

In my virtual host I added...

	<VirtulHost *:80>
	    DocumentRoot "some path"
	    ServerName local.mydomain
	    LogLevel debug
	</VirtualHost>
	

 

Do I have to reboot my Mac?

I shut down MAMP, started it up, ran my web page, and in...

"/Applications/MAMP/logs/pache_error.log"

 

...I don't see anything of value at today's time and date.

 

Did I do that right?

 

And why is the mod_rewrite turning int hell?

 

Neither of the LogLevel directives you show are what I posted.

LogLevel rewrite:trace6

You need to have the correct setting to get any log output.

When changing the httpd.conf you need to stop and start the server for the change to take effect.

Edited by kicken
5 minutes ago, kicken said:

Neither of the LogLevel directives you show are what I posted.


LogLevel rewrite:trace6

You need to have the correct setting to get any log output.

When changing the httpd.conf you need to stop and start the server for the change to take effect.

Where do I put your line of code in the httpd.conf file?

And do I add that in addition to the lines I found above?

Edited by SaranacLake

Replace (or comment out) your current LogLevel in the virtual host.

<VirtulHost *:80>
    DocumentRoot "some path"
    ServerName local.mydomain
    #LogLevel debug
    LogLevel rewrite:trace6
</VirtualHost>

Whenever you get things working, switch it back to what you had before or your log will get big fast.

12 minutes ago, kicken said:

Replace (or comment out) your current LogLevel in the virtual host.


<VirtulHost *:80>
    DocumentRoot "some path"
    ServerName local.mydomain
    #LogLevel debug
    LogLevel rewrite:trace6
</VirtualHost>

Whenever you get things working, switch it back to what you had before or your log will get big fast.

 

That command breaks my webserver and it won't now start.

 

 

8 minutes ago, SaranacLake said:

That command breaks my webserver and it won't now start

It would appear MAMP is using an outdated version of Apache.  Try this instead.

<VirtulHost *:80>
    DocumentRoot "some path"
    ServerName local.mydomain
    LogLevel debug
    RewriteLog "/Applications/MAMP/logs/rewrite.log"
    RewriteLogLevel 6
</VirtualHost>

 

7 minutes ago, kicken said:

It would appear MAMP is using an outdated version of Apache.  Try this instead.


<VirtulHost *:80>
    DocumentRoot "some path"
    ServerName local.mydomain
    LogLevel debug
    RewriteLog "/Applications/MAMP/logs/rewrite.log"
    RewriteLogLevel 6
</VirtualHost>

 

Made the change.

Quit MAMP.

Started it up.

Apache Server will not start.

The phpinfo page says I have Apache/2.2.34

 

 

Edited by SaranacLake

You'll have to do some troubleshooting then and figure out why.  I don't use a Mac, MAMP, or Apache 2.2 (2.4 is current).

As far as I can tell looking at the Apache 2.2 docs that configuration should be valid.

Check your error log, see if it says why it won't start.  Try starting it from a terminal window manually, see if that shows anything.  Try a syntax check on the config file.

Interesting side-note...

In photo-gallery.php, I added...

	echo '$_GET[gallery-id] = ' . $_GET['gallery-id'];
	

and when I run things I get...

	$_GET[gallery-id] = 2019-holiday-party/
	

 

The point?  It is that my pretty URL is getting messed up for reasons still unknown, and the garbage being added onto my pretty URL (i.e. "/?gallery-id=2019-holiday-party") is breaking my code when I run things in that the $_GET value is not what I am expecting.

 

10 minutes ago, kicken said:

You'll have to do some troubleshooting then and figure out why.  I don't use a Mac, MAMP, or Apache 2.2 (2.4 is current).

As far as I can tell looking at the Apache 2.2 docs that configuration should be valid.

Check your error log, see if it says why it won't start.  Try starting it from a terminal window manually, see if that shows anything.  Try a syntax check on the config file.

I am looking in the log now and I see lots of errors but the wrap and its hard to make heads or tails.

 

See lots of "File does not exist" and <path>/public_html/gallery

Also some [error][client 127.0.0.1]Directory index forbidden by Options directive: <path>/public_html/pnc/

 

I guess that is when things wouldn't start up?

 

If I comment out the lines you gae me, MAMP and my scripts run okay except the URL is not right - as always - and as I last posted that messes up my $_GET which isn't life-ending, but still.

I just don't understand why a simple rewrite is causing all of these issues...  *sigh*

Edited by SaranacLake

CORRECTION:  The errors I posted above were from yesterday and probably related to dev errors.

I do NOT see any error from today when MAMP was hanging up.

So while the code @kicken didn't seem to work in my MAMP, nothing as such got logged.

I say this is at the .htaccess level or coding level and not in Apache itself, but who knows?!

:shrug:

Edited by SaranacLake

I got logging to work for MAMP w Apache 2.2...

	#Loglevel debug
	RewriteLog "/Applications/MAMP/logs/rewrite2.log"
	RewriteLevel 4
	

 

To be honest, the log file makes very little sense to me.  Seems like it is going over the same things in my htaccess file over and over again.  I don't see where it s appending on the mysterious /?gallery-id=2019-holiday-party

I checked my mod_rewrite to make sure the last line of every block has an [L] so things don't bleed over.

I still think this is a mod_rewrite issue or possibly something in the code of one of my files and not an Apache misconfiguration.

Sure could use some help solving this very annoying issue!  :cry:

38 minutes ago, SaranacLake said:

To be honest, the log file makes very little sense to me

You could post the log output.

To keep it brief, the best thing to do would be to stop the server, make sure the log file is empty, start the server and run the CURL command to request your page.  That way the log file will only contain the details from that single request and be easier to follow.

 

31 minutes ago, kicken said:

You could post the log output.

To keep it brief, the best thing to do would be to stop the server, make sure the log file is empty, start the server and run the CURL command to request your page.  That way the log file will only contain the details from that single request and be easier to follow.

 

I know you are trying to help, but I'd sorta prefer not to do that since the log file contains sensitive info and it would be a pain to have to scrub it out.

I feel like the answer is right in front of me, although I'm sure this thread has scared almost everyone else away.  ;-(

Here is a recap of where I currently am...

.htaccess

	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteRule client1/gallery/(.*) client1/gallery/photo-gallery.php?gallery-id=$1 [L]
	

 

menu.php

	<a href="/client1/gallery/2019-holiday-party">Holiday party (2019)</a>
	

 

photo-gallery.php

	echo '$_GET[gallery-id] = ' . $_GET['gallery-id'];
	exit();
	

 

URLs:

   http://local.mydomain/client1/menu
    http://local.mydomain/client1/gallery/2019-holiday-party /?=2019-holiday-party

 

Output

Quote

$_GET[gallery-id] = 2019-holiday-party/

 

 

 

Notice that trailing backslash in RED that I have highlighted above!!!

 

Final URL after rewrite:

Quote

 

 

 

Edited by SaranacLake
Fixed known type-o's
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.