Solved

mod_rewrite help

Posted on 2010-08-19
26
426 Views
Last Modified: 2012-05-10
preownedweddingdresses dot ca/?designer=Watters (one word)
preownedweddingdresses dot ca/?designer=Vera+Wang (two words)

to SEO friendly please

preownedweddingdresses dot ca/designer/Watters (one word)
preownedweddingdresses dot ca/designer/Vera-Wang (two words)

What should my .htaccess file look like?
0
Comment
Question by:UltraFlux
  • 13
  • 10
  • 2
  • +1
26 Comments
 
LVL 4

Expert Comment

by:philkeene
Comment Utility
How's this?...


Options +FollowSymlinks
RewriteEngine on

RewriteRule - +
RewriteRule ^designer/(.+)$ ?designer=$1
0
 

Author Comment

by:UltraFlux
Comment Utility
nope, breaks website and shows strange pages that don't exist.
0
 
LVL 4

Expert Comment

by:philkeene
Comment Utility
k try the following then... it should get you half way...

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^designer/(.+)$ ?designer=$1
0
 

Author Comment

by:UltraFlux
Comment Utility
Ok, I see what it is doing.

It's sending me to a URL with Watters but the wrong URL.
/canada/british-columbia/watters-brides-style-1059b/

Should be:
/?designer=Watters   ->   /designer/Watters
0
 
LVL 4

Expert Comment

by:philkeene
Comment Utility
Hmm it seems there is something else going on here. What are you putting in as the URL? /designer/Watters ?
0
 
LVL 4

Expert Comment

by:philkeene
Comment Utility
Also does the index page automaticallly redirect at all?
0
 

Author Comment

by:UltraFlux
Comment Utility
0
 
LVL 4

Expert Comment

by:philkeene
Comment Utility
Is it possible for you to post the code for your index page?
If not have a look for any lines containing something like the following...
header("Location: some_url.com");
0
 
LVL 4

Expert Comment

by:philkeene
Comment Utility
One final suggestion is replace the htaccess line...
RewriteRule ^designer/(.+)$ ?designer=$1
with...
 RewriteRule ^designer/(.+)$ /?designer=$1
0
 

Author Comment

by:UltraFlux
Comment Utility
Still loads same URL.
http://preownedweddingdresses.ca/canada/british-columbia/watters-brides-style-1059b/

Index is from Wordpress, not sure it will be much help...
0
 
LVL 11

Expert Comment

by:mattibutt
Comment Utility
Hi
If you are hosting on share server check you have the permission to use rewrite module if its works then let us know
Thanks
0
 

Author Comment

by:UltraFlux
Comment Utility
It works. I've used it on other domains.
0
 
LVL 4

Expert Comment

by:philkeene
Comment Utility
Would it be possible to include the following PHP in your index file...
 
<!-- FILENAME: <?=$_SERVER['SCRIPT_FILENAME'];?> -->
<!-- QUERY STRING: <?=$_SERVER['QUERY_STRING'];?> -->
 
... that should mean that we can view the source to see what's happening, but won't screw up your web page in the mean time.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 4

Expert Comment

by:philkeene
Comment Utility
Also... could you post the full htaccess code that you have if that is possible.
0
 

Author Comment

by:UltraFlux
Comment Utility
Note: Total Cache is Disabled until I can resolve the above problem.

Included the above in the index.php file.
# BEGIN W3 Total Cache

<IfModule mod_setenvif.c>

    SetEnvIfNoCase Accept-Encoding (gzip|deflate) APPEND_EXT=.$1

</IfModule>

<IfModule mod_rewrite.c>

    RewriteEngine On

    RewriteCond %{REQUEST_URI} \/$

    RewriteCond %{REQUEST_URI} !(\/wp-admin\/|\/xmlrpc.php|\/wp-(app|cron|login|register).php|wp-.*\.php|index\.php) [OR]

    RewriteCond %{REQUEST_URI} (wp-comments-popup\.php|wp-links-opml\.php|wp-locations\.php) [NC]

    RewriteCond %{REQUEST_METHOD} !=POST

    RewriteCond %{QUERY_STRING} =""

    RewriteCond %{HTTP_COOKIE} !(comment_author|wp-postpass|wordpress_\[a-f0-9\]\+|wordpress_logged_in) [NC]

    RewriteCond %{HTTP_USER_AGENT} !(bot|ia_archive|slurp|crawl|spider) [NC]

    RewriteCond /nfs/c01/h14/mnt/13403/domains/preownedweddingdresses.ca/html/wp-content/w3tc/pgcache/$1/_default_.html%{ENV:APPEND_EXT} -f

    RewriteRule (.*) wp-content/w3tc/pgcache/$1/_default_.html%{ENV:APPEND_EXT} [L]

</IfModule>

# END W3 Total Cache



<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]

</IfModule>



# END WordPress

AddHandler php5-script .php



ExpiresActive On

ExpiresByType image/gif A2592000

ExpiresByType image/png AA2592000

ExpiresByType image/jpg AA2592000

ExpiresByType image/jpeg AA2592000



Options +FollowSymlinks

RewriteEngine on



RewriteRule ^designer/(.+)$ /?designer=$1

Open in new window

0
 
LVL 4

Expert Comment

by:philkeene
Comment Utility
Could you try moving lines 36-39 to the very top of the file. I don't see how the other rewrite rules would affect what we are trying to achieve, but it is one way to try to rule it out.
Another thing would be to temporarily back up the htaccess file, and only include lines 36-39 in the live one, for a moment just to see if it works.
Don't forget to put the current one back after testing tho.
0
 

Author Comment

by:UltraFlux
Comment Utility
36 - 39 at top of file

http://preownedweddingdresses.ca/designer/Watters
loads to
http://preownedweddingdresses.ca/canada/british-columbia/watters-brides-style-1059b/

http://preownedweddingdresses.ca/designer/Demitrios
loads to
http://preownedweddingdresses.ca/canada/british-columbia/demitrios-ivory-size-14/

36 - 39 only [ Nothing works ]
http://preownedweddingdresses.ca/designer/Demitrios

Parse error: syntax error, unexpected '&', expecting T_VARIABLE or '$' in /nfs/c01/h14/mnt/13403/domains/preownedweddingdresses.ca/html/wp-content/plugins/custom-field-taxonomies/main.php on line 62

0
 
LVL 4

Expert Comment

by:philkeene
Comment Utility
OK, I have simulated it all on my laptop. I have a simple index.php file...
 
-------------------------
FILENAME: <?=$_SERVER['SCRIPT_FILENAME'];?><br/><br/>
QUERY STRING: <?=$_SERVER['QUERY_STRING'];?>

-------------------------
 
and I have used your .htaccess file, (with lines 30-34 commented out - I think I must be missing a module in my PHP or something because that was causing problems).
 
-------------------------
# BEGIN W3 Total Cache
<IfModule mod_setenvif.c>
    SetEnvIfNoCase Accept-Encoding (gzip|deflate) APPEND_EXT=.$1
</IfModule>
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} \/$
    RewriteCond %{REQUEST_URI} !(\/wp-admin\/|\/xmlrpc.php|\/wp-(app|cron|login|register).php|wp-.*\.php|index\.php) [OR]
    RewriteCond %{REQUEST_URI} (wp-comments-popup\.php|wp-links-opml\.php|wp-locations\.php) [NC]
    RewriteCond %{REQUEST_METHOD} !=POST
    RewriteCond %{QUERY_STRING} =""
    RewriteCond %{HTTP_COOKIE} !(comment_author|wp-postpass|wordpress_\[a-f0-9\]\+|wordpress_logged_in) [NC]
    RewriteCond %{HTTP_USER_AGENT} !(bot|ia_archive|slurp|crawl|spider) [NC]
    RewriteCond /nfs/c01/h14/mnt/13403/domains/preownedweddingdresses.ca/html/wp-content/w3tc/pgcache/$1/_default_.html%{ENV:APPEND_EXT} -f
    RewriteRule (.*) wp-content/w3tc/pgcache/$1/_default_.html%{ENV:APPEND_EXT} [L]
</IfModule>
# END W3 Total Cache
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
AddHandler php5-script .php
#ExpiresActive On
#ExpiresByType image/gif A2592000
#ExpiresByType image/png AA2592000
#ExpiresByType image/jpg AA2592000
#ExpiresByType image/jpeg AA2592000
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^designer/(.+)$ ?designer=$1
-------------------------
 
It gives the correct expected results...
http://localhost/testing_the_file/designer/Watter
gives...
 
FILENAME: C:/Program Files/EasyPHP 3.0/www/testing_the_file/index.php

QUERY STRING: designer=Watter
 
Which shows the htaccess is working fine. I think we need to investigate whether something else is redirecting at any point. Does the designer folder actually exist?
0
 
LVL 4

Expert Comment

by:philkeene
Comment Utility
One other thing... where did you put the htaccess file? is it in the following folder?
/home/13403/domains/preownedweddingdresses.ca/html/
 
0
 

Author Comment

by:UltraFlux
Comment Utility
Sorry for my absence

Yes, htaccess is in the root /html/

Ok used the above code.

Still end up in the same place
http://preownedweddingdresses.ca/designer/Watters
redirects to
http://preownedweddingdresses.ca/canada/british-columbia/watters-brides-style-1059b/


Query String actually used:
http://preownedweddingdresses.ca/?designer=Watters
http://preownedweddingdresses.ca/?designer=Vera+Wang
*note the + with two words
0
 
LVL 26

Expert Comment

by:arober11
Comment Utility
Hi, the following, unknown file or directory, rule will be encountered before and over-ride your "designer" rule:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

So simply move the rule before this rule e.g

#----- cut here -----
# BEGIN W3 Total Cache
<IfModule mod_setenvif.c>
    SetEnvIfNoCase Accept-Encoding (gzip|deflate) APPEND_EXT=.$1
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} \/$
    RewriteCond %{REQUEST_URI} !(\/wp-admin\/|\/xmlrpc.php|\/wp-(app|cron|login|register).php|wp-.*\.php|index\.php) [OR]
    RewriteCond %{REQUEST_URI} (wp-comments-popup\.php|wp-links-opml\.php|wp-locations\.php) [NC]
    RewriteCond %{REQUEST_METHOD} !=POST
    RewriteCond %{QUERY_STRING} =""
    RewriteCond %{HTTP_COOKIE} !(comment_author|wp-postpass|wordpress_\[a-f0-9\]\+|wordpress_logged_in) [NC]
    RewriteCond %{HTTP_USER_AGENT} !(bot|ia_archive|slurp|crawl|spider) [NC]
    RewriteCond /nfs/c01/h14/mnt/13403/domains/preownedweddingdresses.ca/html/wp-content/w3tc/pgcache/$1/_default_.html%{ENV:APPEND_EXT} -f
    RewriteRule (.*) wp-content/w3tc/pgcache/$1/_default_.html%{ENV:APPEND_EXT} [L]

# Designer Rule
  RewriteRule ^designer/(.+)$ /?designer=$1   [L,QSA]

# Default non existent file or directory rule:
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.php [L]
</IfModule>

# END WordPress
AddHandler php5-script .php

ExpiresActive On
ExpiresByType image/gif A2592000
ExpiresByType image/png AA2592000
ExpiresByType image/jpg AA2592000
ExpiresByType image/jpeg AA2592000

Options +FollowSymlinks
#----- cut ends -----

If you also want / need to translate a '-' into a '+' sign, as part of the translation. Per you original post:

> preownedweddingdresses dot ca/?designer=Vera+Wang (two words)
>
> to SEO friendly please
>
> preownedweddingdresses dot ca/designer/Vera-Wang (two words)

Then alter the Designer rule, in it's new location to:

#----- Cut here -----
# Designer Rules
  RewriteRule ^designer/([^-]+)$          /?designer=$1         [L,QSA]
  RewriteRule ^designer/([^-]+)-([^-]+)$ /?designer=$1+$2 [L,QSA]
#----- Cut ends -----
0
 

Author Comment

by:UltraFlux
Comment Utility
0
 
LVL 26

Expert Comment

by:arober11
Comment Utility
Fairly simple to explain you either have a couple more re-write rules, or a default handler that are encountered before the rules above and:
a) First of all, rewrites the URL add a trailing '/' to the end.
b) Secondly, rewrites the modified URL's as follows:

/designer/Watters/      ->  /canada/british-columbia/watters-brides-style-1059b/
/designer/Vera-Wang/ -> /canada/ontario/vera-wang-genuine-silk-size-8/

See: http://httpd.apache.org/docs/2.2/mod/mod_dir.html#directoryslash

e.g. Using Firefox and the "Live HTTP Headers" plug-in, a request for http://preownedweddingdresses.ca/designer/Watters results in:

#------------------------------------
GET /designer/Watters HTTP/1.1
Host: preownedweddingdresses.ca
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8
...

HTTP/1.1 302 Moved Temporarily
Date: Mon, 06 Sep 2010 22:52:51 GMT
Server: Apache/2.0.54
Location: http://preownedweddingdresses.ca/designer/Watters/

#------------------------------------

GET /designer/Watters/ HTTP/1.1
Host: preownedweddingdresses.ca
...

HTTP/1.1 302 Moved Temporarily
Date: Mon, 06 Sep 2010 23:04:02 GMT
Server: Apache/2.0.54
Location: http://preownedweddingdresses.ca/canada/british-columbia/watters-brides-style-1059b/

#---------------------------------


If you have access to httpd.conf have a look to see what's there for your virtualhost, as there is nothing the the code above that will cause the re-writes.
0
 
LVL 4

Expert Comment

by:philkeene
Comment Utility
Does the /designer/Watters folder actually exist? and if so is there an index file or an .htaccess file in there?
0
 

Author Comment

by:UltraFlux
Comment Utility
no physical directory
0
 
LVL 4

Accepted Solution

by:
philkeene earned 500 total points
Comment Utility
Sorry mate I think I'm stumped... Have you tried the same code on a different server... you could download this to your PC if you don't have a free server (www.easyphp.org).
That may help with diagnosing the problem, by trying to recreate it. It would also mean you could break the code when its on the different server without worrying too much.
Not sure if that is much help, but other than that, I can't really see any reason for the behaviour.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now