AEM: URL Shortening for assets - mod-rewrite

I am new to AEM rewrite rules. I have two URLs
/content/dam/app/document/folder/filename.docx
/document/folder/filename or /document/filename
I want to shorten the first URL to second URL using Rewrite Rules in Apache. But am not exactly sure on how to do it. Is there any solution to this?
So far I have written
RewriteCond %{REQUEST_URI} ./document/.
RewriteRule (.*)$ $1.docx
This appends the extension at the end but /content/dam/app/ needs to be appended

Related

mod-rewrite rules executing out of order

Okay, so I have two separate mod-rewrite rules in my vhost block. The first rule redirects a customer offsite if they come in thru an affiliate URL such as example.com/1234.html and the second rule forces the URL to always contain www dot like www.example.com.
# Affiliate Links
RewriteRule ^([0-9]+)\.html$ http://affiliates.example.com/log.php?id=$1 [R=302,L]
# Ensure we are always on www dot
RewriteCond %{HTTP_HOST} ^example\.loc [NC]
RewriteRule (.*) http://www.example.com$1 [R=301,L]
The rules themselves work great. The problem is that if the first rule applies I want it to immediately redirect, however it seems as if the second rule is hoisted to the top because it always takes precedence. What do I need to change so that these execute in order?
You've stated that this is in a vhost block. In that context (as opposed to, for example, an .htaccess file) URLs always start with '/'
Thus
RewriteRule ^([0-9]+)\.html$ http://affiliates.example.com/log.php?id=$1 [R=302,L]
should instead be
RewriteRule ^/([0-9]+)\.html$ http://affiliates.example.com/log.php?id=$1 [R=302,L]
(ie, with the leading slash), otherwise it will never match anything.

RewriteRule domain which contains '?'

I want to have RewriteRule where domain's /page/test or /page/test/ go to www.domain.com. Only for this two example.
I try:
RewriteRule ^/page/test/?$ http://www.domain.com [R=301,L,NC]
This will be good but I have other pages like /page/test/?D_test
and when I put RewriteRule, redirect also /page/test/?D_test. Problem is '?'. How should I write my rule to don't rewrite another page's to www.domain.com ? for example
/page/test/?D_test /page/test/(.*)
I'm not sure I understand your question. You are either asking how can I redirect all the traffic including the query string to the domain and take off the query string. In that case I usually do it this way:
RewriteRule ^/page/test/?$ http://www.domain.com? [R=301,L,NC]
If you want to make it so it doesn't redirect if there is a query string then you have to use a rewrite conditional.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/page/test/?$ http://www.domain.com? [R=301,L,NC]

mod_rewrite, change one URL with query to a completely different URL

I am migrating data from one content management system to another. There is no relationship between old URLs and new URLs, although both contain query strings. I am trying to set up a set a rewrites that will redirect broad category lists of data from one to the other.
Here's a sample:
OLD
rss.php?categoryID=53
NEW
index.php?module=news&type=user&func=list&tid=1&filter=blogtopic:eq:19
I tried
RewriteRule ^rss.php\?categoryID=53 index.php?module=news&type=user&func=list&tid=1&filter=blogtopic:eq:19 [L]
but it doesn't match. If I follow that one with
RewriteRule ^rss.php index.php?module=news&type=user&func=list&tid=1 [L]
if DOES match, so I conclude that the question mark in the old URL is causing the problem. I am already escaping the question mark. What do I do?
I will probably end up with about 50 of these in my .htaccess file.
You can't match against the query string (all that stuff after the ?) in a RewriteRule, you need to use a RewriteCond and match against the `%{QUERY_STRING} var:
RewriteCond %{QUERY_STRING} ^categoryID=53$
RewriteRule ^rss\.php$ /index.php?module=news&type=user&func=list&tid=1&filter=blogtopic:eq:19 [L]
Or change the brackets to [R=301,L] if you want to redirect the browser.

Basic help with mod_rewrite conditions

Hi. I am new to mod_rewrite and was wondering if it was possible to do the following:
RewriteRule ^([^/]*)$ index.php?slug=$1
This rule will point only at index.php but if I wanted to do another rule that pointed a specific slug to a different script, i.e.
RewriteRule ^a-new-page$ different.php
This would not work because the first rule has declared anything that is entered should point at index.
Is there a way to force the new rule for that specific slug?
Yes, it is possible -- just place such specific rule before generic/broad one (the order in which rules are declared matters):
RewriteRule ^a-new-page$ different.php
RewriteRule ^([^/]*)$ index.php?slug=$1
Also please pay attention to the broad rules -- they may enter into a rewrite loop (after rewrite new URL goes into next iteration, and if rule is not written right it may enter endless loop which Apache will have to forcedly terminate and your user will see an 500 error page instead). Better rule will be:
# specific rule
RewriteRule ^a-new-page$ different.php [L,QSA]
# broad rule (will be triggered if requested resource is not a file or directory)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ index.php?slug=$1 [L,QSA]
[L] tells Apache to stop rewriting if this rule matches
[QSA] tells to append query string to a new URL (useful if you have URL parameters: e.g. tracking data, page parameters etc).
Useful Link: http://httpd.apache.org/docs/current/rewrite/

mod_Rewrite: Filter specific pages by IP and redirect them

I am researching a way to work filtering specific pages by IP and redirect them on a different page.
The code below, did not work properly.
RewriteCond %{REMOTE_ADDR} ^/192.168.10.*
RewriteCond %{REQUEST_URI} ^/support
RewriteRule ^/.* http://www.yahoo.com/gone [R,NE]
Once the link http://example.com/support has been accessed and they're on the 192.168.10.* block, it must go to the yahoo.com example page.
But, like I said. It just did nothing. Any ideas why it did not work correctly?
as yoda says in the comment, don't put a / in front of the ip address. also, the . in the pattern should be \., as this is a perl compatible regular expression. you could also add a [NC], no case (sensitive), to the request uri match. finally, you could merge the second condition with the RewriteRule. all together:
RewriteCond %{REMOTE_ADDR} ^192\.168\.10\..*
RewriteRule ^/support http://www.yahoo.com/gone [R,NE,NC]

Resources