MVC urlHelper.RouteUrl to provide the full path on the server - asp.net

I'm currently doing something in OrchardCMS we're using URLRewrite so that if a user comes to the site from site.com or from site.com/orchard, they get the site.com/orchard page.
One of the problems is that it's throwing the urls in the menu because it's using urlHelper.RouteUrl which uses the wrong context and then produces a url like site.com/page1 rather than site.com/orchard/page1. Is there either a way to tell urlHelper.RouteUrl to start from a different page than the current one, or to get the function to return the full path?
Thanks

Maybe you could use the Orchard Rewrite module where you can define redirect rules like this:
RewriteCond %{REQUEST_URI} !^orchard [NC]
RewriteRule ^(.*)$ /orchard/$1 [R=301,L]

Try setting up your application under site.com/orchard virtual directory and then use UrlRewrite in IIS to do appropriate 301 redirect when user enters site.com. This is a much cleaner approach.
HTH

Related

htaccess: If the requested URL doesn't contain a specific string, redirect user to different domain while maintaining the slug

I have looked through quite a few different posts on here without any luck.
I have two sites (for example): exampledomain.com and mb.exampledomain.com
If a user accesses mb.exampledomain.com and the URL doesn't contain the string "retreat", then I want to redirect them to exampledomain.com while maintaining the slug.
So:
mb.exampledomain.com/retreat (or) mb.exampledomain.com/retreat_sunday should be accessible to the user as normal
mb.exampledomain.com/ (or) mb.exampledomain.com/about-us should redirect to exampledomain.com/ (or) exampledomain.com/about-us respectively.
I need to achieve this by using rules in a .htaccess file at the root of the directory.
Additionally, the htaccess rule shouldn't impact the accessibility of asset files on the mb.exampledomain.com/retreat page(s).
Thanks for any help you can provide!
Sounds pretty straight forward:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/retreat
RewriteRule ^ https://exampledomain.com%{REQUEST_URI} [QSA,R=301,END]
That catches all requests to URLs not starting with the literal string "retreat". Which is what you examples demonstrate. If you only want to check for the string itself, regardless of where in the path it occurs, then that should do what you ask:
RewriteEngine on
RewriteCond %{REQUEST_URI} !retreat
RewriteRule ^ https://exampledomain.com%{REQUEST_URI} [QSA,R=301,END]
It is a good idea to start out with a 302 temporary redirect and to only change it into a 301 permanent redirect once you are certain everything works as expected.

Redirect URL with mod_rewrite with variable

I am looking to get a rule setup to change one of my URL's. After much research I understand the basics of mod_rewrite. The trouble I am having is the dynamic URL that is being re written. I have outlined in as much detail possible the task and desired outcome.
I currently have a url that looks like this:
http://www.myndness.com/my-canvas/{username}/
The {Username} is variable depending on which user you click on.
What I need to do is add /?my_posts to the end of that URL. This will then display the posts module by default for all users.
So all the users will follow the same URL pattern:
/User1/?my_posts
/User2/?my_posts
/User3/?my_posts
/User4/?my_posts
Apologies if I have missed anything.
I am using WordPress and have access to all files on the server.
Try this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^my-canvas/[^/]+/?$ $0?my_posts [R=301,L,NC]

Rewrite/redirect aspx with specific quesry string to specific wordpress page

I've searched through and can't find a working solution...
I've ported a site from asp.net to php. As instructed, I left the internal links as .aspx for seo. For static pages outside wordpress, with no query string, I can work it fine:
RewriteEngine on
RewriteRule ^testimonials.aspx$ /testimonials.php [NC,R=301]
But, I have some links which include query strings... These links now have to point to pages that are currently inside a wordpress installation. How can I get
myawesomesite.com/catalog.aspx?n=My%incredible%20product
to redirect to
myawesomesite.com/catalog/my-new-incredible-product/
AND
myawesomesite.com/catalog.aspx?n=My%other%20product
to redirect to
myawesomesite.com/catalog/my-new-other-product/
etc... (in the destination, 'catalog' is the directory that wordpress is installed in)
I've tried all kinds of things but am no expert. I know I need to do something to get apache to catch the query strings...
I'll give this a go the below should do it:
RewriteCond %{QUERY_STRING} ^n=My(?:[\ +]|%20)incredible(?:[\ +]|%20)product$ [NC]
RewriteRule ^catalog\.aspx$ http://www.myawesomesite.com/catalog/my-new-incredible-product/? [R=301,NE,NC,L]
Just amend the parts to match your other URL
I have always found this tool to be useful for redirects
http://seo-website-designer.com/HtAccess-301-Redirect-Generator

mod_rewrite redirect for site that contains drupal

I am new to using mod_rewrite, so I need an explanation in simple terms. I'm trying to do a redirect for a directory that is in the same directory as a Drupal installation but is not part of Drupal.
For example, a file used to exist at http://mysite.com/events/listen/song.mp3. I rearranged so that the file now resides at http://mysite.com/listen/song.mp3.
I can use the following in mod_rewrite and it works:
RewriteRule ^events/listen/(.+)$ listen/$1 [NC]
However, I'd like to do a real redirect, so that the user sees the new URL instead of the old one. I've tried:
RewriteRule ^events/listen/(.+)$ listen/$1 [R,NC]
and
RewriteRule ^events/listen/(.+)$ http://mysite.com/listen/$1 [R,NC]
Neither of these work. I think it probably has to do with all the rerouting that Drupal is doing, though I do have my rewrite rules before Drupal's in .htaccess. Is there something obvious I'm missing? Or is there a way to allow this to work without messing up what Drupal is doing?
Try
RewriteRule ^events/listen/(.+)$ listen/$1 [R=301,L]

How do I create a redirect from a subdirectory to the root domain?

I am trying to redirect all requests to domain.com/drupal to domain.com, including all sub directories in /drupal.
I have seen several answers telling me how to accomplish the opposite of this with .htaccess, but nothing to go this way. I have tried the following line in .htaccess-
RewriteRule /drupal/* ^/(.*)
as well as several variations of the above, based on those answers, but haven't had any luck.
Thanks!
Try this line:
RewriteRule /drupal/(.*) /$1 [QSA,L]
Let me get this straight ... You have an installation of Drupal in <DocumentRoot>/drupal/. You do not want to alter the drupal installation directory, nor you want to change DocumentRoot in your webserver config. You want to redirect any request, for example /foobar.php, into the drupal directory, resulting in maybe /drupal/foobar.php. And all that without exposing the whole stuff to the user. Right so far? OK, I can only assume that you have an Apache webserver, else .htaccess would not work...
First, make sure that you actually are allowed to use .htaccess, so check on the relevant AllowOverride directive in your apache config.
Then try it this way in your <DocumentRoot>/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/drupal.*
RewriteRule ^/(.*)$ /drupal/$1 [P]
RewriteCond ensures that you do not run into an infinite loop. The first part of the RewriteRule is always the URL requested by the client. We prefix the part matched inside the parentheses with /drupal/ and force it to be a proxy request via [P] so that apache would only do an internal redirect (instead of sending the client a "Document has moved" redirection code).
BTW: I did not test it. I may have typos in the code. Read http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriterule for more information.

Resources