Rewrite help for newbie

ISAPI_Rewrite is Apache mod_rewrite compatible URL rewriter for Microsoft IIS
User avatar
Posts: 8
Joined: 23 Jan 2017, 20:42

Rewrite help for newbie

24 Jan 2017, 17:37

I've just been getting started and have had good success so far and have all the features of my asp/asp.net hybrid application functioning without the file extensions. Now I want to hide the querystring names.

Here's my current rewrite rules:

# Helicon ISAPI_Rewrite configuration file
# Version 3.1.0.112

RewriteEngine on

#Disable rewrite on includes sub directories
RewriteRule ^ScriptLibrary - [NC,L]
RewriteRule ^mail_templates - [NC,L]
RewriteRule ^navigation - [NC,L]
RewriteRule ^calendar_ajax - [NC,L]

#Redirect extension requests to avoid duplicate content
RewriteRule ^([^?]+)\.asp$ $1 [NC,R=301,L]

#Internally add extensions to request
RewriteCond %{REQUEST_FILENAME}.asp -f
RewriteRule (.*) $1.asp

#RewriteRule ^product_details~(.*)/Name(.*)\ /product_details.asp?P_ID=$1&Name=$2 [L]
#RewriteRule ^product_details/(.*)/Name(.*)\ /product_details?P_ID=$1&Name=$2 [L]

I've been trying to rewrite a link like this: product_details?P_ID=ODQg&Name=Highway+Products to something like this product_details~ODQg~Highway+Products. I have number of links that take the same form in the application but with a different file name, e.g. product_cat?Category_ID=MzQg&Name=Bed+Covers. I would like to have rule I can adapt to handle each different instance. But so far my efforts have failed.

User avatar
Posts: 402
Joined: 06 Mar 2012, 11:59

Re: Rewrite help for newbie

25 Jan 2017, 10:49

Hello.

If your backend application does not come with built-in SE-friendly URLs system (like Wordpress or Drupal, etc.), normally it takes time to design a new URL system for backend completely. It requires at least one rule per page and knowledge of what parameters are expected by the page, in which order and whether any optional ones.

Here is an example of such rule for your product_details page:

RewriteRule ^product_details/(.*)(?:/(.*))?/? product_details.asp?P_ID=$1(?2&Name=$2) [NC,QSA]

The rule above will take URL in format 'product_details/ODQg/Highway+Products/?optional=query_parameter', always in the same order, the part with "/Highway+Products" is optional and trailing slash it optional as well. Additional parameters can be passed through query string (which is convenient as not all of complicated URL structures actually need SE-friendly appearance).

And remove your "add extensions" rule. It is too general and this what really brakes your other rules. Or at least move it to the bottom of .htaccess so it will not interfere with other rules.

User avatar
Posts: 8
Joined: 23 Jan 2017, 20:42

Re: Rewrite help for newbie

25 Jan 2017, 12:34

I wrote the backend and I've written rules so I can format the URLS in the output as I like. All of that works great. The issue has been writing the .htaccess which is new to me. I did want to avoid using the "/" character in the rewritten URLS, though.

User avatar
Posts: 8
Joined: 23 Jan 2017, 20:42

Re: Rewrite help for newbie

25 Jan 2017, 13:21

It doesn't work. When I use the rule above and try to call a page like this "product_details/ODQg/Highway+Products" I get a 404 with or without the extension.

User avatar
Posts: 402
Joined: 06 Mar 2012, 11:59

Re: Rewrite help for newbie

27 Jan 2017, 10:22

Sorry, I forget about greedy matches. Please try this instead:

RewriteRule ^product_details/([^/]*)(?:/([^/]*))?/? product_details.asp?P_ID=$1(?2&Name=$2) [NC,QSA]

User avatar
Posts: 8
Joined: 23 Jan 2017, 20:42

Re: Rewrite help for newbie

27 Jan 2017, 13:23

Still doesn't work. I get a 404 with a URL formatted like this "product_details/Mzgg/Alpine" or this "product_details.asp/Mzgg/Alpine" I even tried deleting the file extensions rules. Here is the .htaccess:

# Helicon ISAPI_Rewrite configuration file
# Version 3.1.0.112

#RewriteEngine on

#Disable rewrite on includes sub directories
RewriteRule ^ScriptLibrary - [NC,L]
RewriteRule ^mail_templates - [NC,L]
RewriteRule ^navigation - [NC,L]
RewriteRule ^calendar_ajax - [NC,L]

RewriteRule ^product_details/([^/]*)(?:/([^/]*))?/? product_details.asp?P_ID=$1(?2&Name=$2) [NC,QSA]

My web application is here: http://wwwebconcepts.com/datasite_catalog/asp/default

User avatar
Posts: 402
Joined: 06 Mar 2012, 11:59

Re: Rewrite help for newbie

27 Jan 2017, 14:30

I have tested this rule and it worked exactly as required. I, however, use Helicon Ape instead of ISAPI_rewrite. Although Ape offers much more, URL rewriting features are very similar and same rules should work for both products. Can you please check your IIS log (since ISAPI_Rewrite does not have rule debugger) for what you really get requested when you enter the test URL.

Also please try changing rule to this:

RewriteRule ^product_details/([^/]*)/([^/]*)/? product_details.asp\?P_ID=$1&Name=$2 [NC,QSA]

In rule above I removed the optional part, to check it there could be a problem.

User avatar
Posts: 8
Joined: 23 Jan 2017, 20:42

Re: Rewrite help for newbie

27 Jan 2017, 14:36

With this .htaccess:

# Helicon ISAPI_Rewrite configuration file
# Version 3.1.0.112

#RewriteEngine on

#Disable rewrite on includes sub directories
RewriteRule ^ScriptLibrary - [NC,L]
RewriteRule ^mail_templates - [NC,L]
RewriteRule ^navigation - [NC,L]
RewriteRule ^calendar_ajax - [NC,L]

RewriteRule ^product_details/([^/]*)/([^/]*)/? product_details.asp\?P_ID=$1&Name=$2 [NC,QSA]


Attempts to cal the page all get a 404 with or without the file extension. product_details/ODQg/Highway+Products = 404 and product_details.asp/ODQg/Highway+Products also = 404.

User avatar
Posts: 402
Joined: 06 Mar 2012, 11:59

Re: Rewrite help for newbie

27 Jan 2017, 15:03

Please, open your IIS log file and read the records corresponding to your test requests. Send these records here.

User avatar
Posts: 8
Joined: 23 Jan 2017, 20:42

Re: Rewrite help for newbie

27 Jan 2017, 15:28

Here's the log I created trying different combinations.
Attachments
log.txt
(51.79 KiB) Downloaded 1646 times

User avatar
Posts: 402
Joined: 06 Mar 2012, 11:59

Re: Rewrite help for newbie

02 Feb 2017, 11:40

Hello.

I see you forgot to mention that /product_details.asp page is actually /asp/datasite_catalog/asp/product_details.asp, that is why the rule didn't match. Please use:


Code: Select all
RewriteEngine on

#Disable rewrite on includes sub directories
RewriteRule ^ScriptLibrary - [NC,L]
RewriteRule ^mail_templates - [NC,L]
RewriteRule ^navigation - [NC,L]
RewriteRule ^calendar_ajax - [NC,L]

RewriteRule ^asp/datasite_catalog/asp/product_details/([^/]*)/([^/]*)/? asp/datasite_catalog/asp/product_details.asp\?P_ID=$1&Name=$2 [NC,QSA]


If you want a shorter URL, like just /product_details/... please use:

Code: Select all
RewriteRule ^product_details/([^/]*)/([^/]*)/? /asp/datasite_catalog/asp/product_details.asp\?P_ID=$1&Name=$2 [NC,QSA]



Anyway the real page is in subdir and that is where you need to refer to it.

User avatar
Posts: 8
Joined: 23 Jan 2017, 20:42

Re: Rewrite help for newbie

03 Feb 2017, 21:06

Thanks, we're making progress.

The folder datasite_catalog is its own IIS application and the .htaccess is in the root folder of the application datasite_catalog. For simplicity, I redefined the root folder as I still couldn't get it to work with the full path. And I modified the rule to:

RewriteRule ^asp/product_details/([^/]*)/([^/]*)/? asp/product_details.asp\?P_ID=$1&Name=$2 [NC,QSA]

Now I have it working, after a fashion, but I want to ask for clarity if I'm getting the expected behavior.

If I call the page http://192.168.1.46/asp/product_details/OTUg/DeeZee NXt Black Board Black Trim it does resolve. The unexpected part is the address bar shows http://192.168.1.46/asp/product_details ... JUQmPkrI-U instead of the pretty URL http://192.168.1.46/asp/product_details/OTUg/DeeZee NXt Black Board Black Trim.

Is this expected?

Is there anyway to insure the address bar shows the pretty URL?

User avatar
Posts: 8
Joined: 23 Jan 2017, 20:42

Re: Rewrite help for newbie

06 Feb 2017, 20:45

Here's my .htaccess

# Helicon ISAPI_Rewrite configuration file
# Version 3.1.0.112
# WWWeb Concepts DataSite WCMS Catalog Version 8.00
# Rewrite rules .htaccess version 1.0 2/5/2017
# James W. Threadgill wwwebconcepts.com

#RewriteEngine on

#Disable rewrite on includes sub directories
RewriteRule ^ScriptLibrary - [NC,L]
RewriteRule ^mail_templates - [NC,L]
RewriteRule ^navigation - [NC,L]
RewriteRule ^calendar_ajax - [NC,L]

#Catalog URLs
RewriteRule ^asp/product_cat/([^/]*)/([^/]*)/? asp/product_cat.asp\?Category_ID=$1&Name=$2 [NC,QSA]
RewriteRule ^asp/subcategories/([^/]*)/([^/]*)/? asp/subcategories.asp\?Category_ID=$1&Name=$2 [NC,QSA]
RewriteRule ^asp/product_details/([^/]*)/([^/]*)/? asp/product_details.asp\?P_ID=$1&Name=$2 [NC,QSA]

#Features URLs
RewriteRule ^asp/blog_post/([^/]*)/([^/]*)/? asp/blog_post.asp\?Content_ID=$1&Name=$2 [NC,QSA]
RewriteRule ^asp/news_details/([^/]*)/([^/]*)/? asp/news_details.asp\?N_ID=$1&Name=$2 [NC,QSA]
RewriteRule ^asp/staff_profile/([^/]*)/([^/]*)/? asp/staff_profile.asp\?Staff_ID=$1&Name=$2 [NC,QSA]

#Miscellaneous URLs
RewriteRule ^asp/information-1/([^/]*)/? asp/information-1.asp\?Name=$1 [NC,QSA]
RewriteRule ^asp/information-2/([^/]*)/? asp/information-2.asp\?Name=$1 [NC,QSA]
RewriteRule ^asp/information-3/([^/]*)/? asp/information-3.asp\?Name=$1 [NC,QSA]
RewriteRule ^asp/catalog_home/([^/]*)/? asp/catalog_home.asp\?Name=$1 [NC,QSA]
RewriteRule ^asp/news/([^/]*)/? asp/news.asp\?Name=$1 [NC,QSA]
RewriteRule ^asp/blog/([^/]*)/? asp/blog.asp\?Name=$1 [NC,QSA]
RewriteRule ^asp/video_vault/([^/]*)/? asp/video_vault.asp\?Name=$1 [NC,QSA]
RewriteRule ^asp/locations/([^/]*)/? asp/locations.asp\?Name=$1 [NC,QSA]
RewriteRule ^asp/links/([^/]*)/? asp/links.asp\?Name=$1 [NC,QSA]
RewriteRule ^asp/staff/([^/]*)/? asp/staff.asp\?Name=$1 [NC,QSA]

#Redirect extension requests to avoid duplicate content
RewriteRule ^([^?]+)\.asp$ $1 [NC,R=301,L]
RewriteCond %{REQUEST_FILENAME}.asp -f
RewriteRule (.*) $1.asp

User avatar
Posts: 402
Joined: 06 Mar 2012, 11:59

Re: Rewrite help for newbie

07 Feb 2017, 08:40

If the URL displayed in address bar changes and your rule does not contain [R] (Redirect) flag, then redirect is issued somewhere else. Most likely redirect is a part of your script operation. Check its code for possible redirect commands.

Return to ISAPI_Rewrite 3.0

Who is online

Users browsing this forum: No registered users and 18 guests