Solved

mod_rewrite help

Posted on 2010-08-19
26
432 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 10
  • 2
  • +1
26 Comments
 
LVL 4

Expert Comment

by:philkeene
ID: 33479485
How's this?...


Options +FollowSymlinks
RewriteEngine on

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

Author Comment

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

Expert Comment

by:philkeene
ID: 33479732
k try the following then... it should get you half way...

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^designer/(.+)$ ?designer=$1
0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

Author Comment

by:UltraFlux
ID: 33479888
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
ID: 33479919
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
ID: 33479931
Also does the index page automaticallly redirect at all?
0
 
LVL 4

Expert Comment

by:philkeene
ID: 33481054
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
ID: 33481060
One final suggestion is replace the htaccess line...
RewriteRule ^designer/(.+)$ ?designer=$1
with...
 RewriteRule ^designer/(.+)$ /?designer=$1
0
 

Author Comment

by:UltraFlux
ID: 33481132
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
ID: 33482937
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
ID: 33486031
It works. I've used it on other domains.
0
 
LVL 4

Expert Comment

by:philkeene
ID: 33488947
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
 
LVL 4

Expert Comment

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

Author Comment

by:UltraFlux
ID: 33488994
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
ID: 33489075
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
ID: 33489955
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
ID: 33491059
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
ID: 33491074
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
ID: 33544339
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
ID: 33604441
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
ID: 33612185
0
 
LVL 26

Expert Comment

by:arober11
ID: 33614090
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
ID: 33619511
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
ID: 33728924
no physical directory
0
 
LVL 4

Accepted Solution

by:
philkeene earned 500 total points
ID: 33732465
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

691 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